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.
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")." "; } } ?>
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]." "; } ?>
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"] } ?>
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." "; } ?>
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!