How to query the results in php

藏色散人
Release: 2023-03-17 06:38:02
Original
1965 people have browsed it

How to query results in php: 1. Obtain a row of elements in the result data set through the mysql_result() function; 2. Use the number as an attribute index to obtain the attribute value through the mysql_fetch_row() function; 3. Use mysql_fetch_array() The function directly obtains the attribute value; 4. Analyze the query results through the mysql_fetch_object() function.

How to query the results in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How does php analyze the query results?

Analysis of four query return results in PHP development, friends in need can refer to it.

1.

$connection=mysql_connect("localhost","root","password"); //连接并选择数据库服务器
mysql_select_db("test",$connection);
$query="insert into users(user_name)"; //在test数据库里插入一条数据
$query.="values('tuxiaohui')";
$result=mysql_query($query);
if(!$query)
echo "insert data failed!
";
else{
$query="select * from users"; //查询数据
$result=mysql_query($query,$connection);
for($rows_count=0;$rows_count<7;$rows_count++) //用mysql_result获得数据并输出,mysql_result() 返回 MySQL 结果集中一个单元的内容。
{
echo "用户ID:".mysql_result($result,$rows_count,"user_id")."
";
echo "用户名:".mysql_result($result,$rows_count,"user_name")."
";
}
}
?>
Copy after login

2.

$connection=mysql_connect("localhost","root","password"); //连接并选择数据库服务器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_row($result))
{
echo "用户ID:".$row[0]."
";
echo "用户名:".$row[1]."
";
}
?>
Copy after login

3.

$connection=mysql_connect("localhost","root","password"); //连接并选择数据库服务器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_array($result))
{
echo "用户ID:".$row[0]."
"; //也可以写做$row["user_id"]
echo "用户名:".$row[1]."
"; //也可以写做$row["user_name"]
}
?>
Copy after login

4.

$connection=mysql_connect("localhost","root","root"); //连接并选择数据库服务器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_object($result))
{
echo "用户ID:".$row->user_id."
"; //通过对象运算符->获得改行数据在其属性上的值。
echo "用户名:".$row->user_name."
";
}
?>
Copy after login

5.Comprehensive comparison:

mysql_result(): The advantage is that it is easy to use; its disadvantage is that it has few functions. One call can only obtain one row of elements in the result data set, which is less efficient for larger databases;

mysql_fetch_row(): Advantages The execution efficiency is the highest among the four methods; the disadvantage is that only numbers can be used as attribute indexes to obtain attribute values, which is very easy to cause confusion when using them;

mysql_fetch_array(): The execution efficiency is equally high, same as mysql_fetch_row( ) is almost the same, and the attribute value can be obtained directly by using the attribute name, so it is most commonly used in practical applications;

mysql_fetch_object(): It adopts object-oriented thinking and is more advanced in design ideas. If you are used to it If you use object-oriented ideas to write programs, you will naturally choose it. Secondly, the advantage of this method is that the data results with a more responsible structure are logically clearer.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to query the results in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template