Four queries in PHP development return result analysis

  • 2020-03-31 21:28:55
  • OfStack

1. < ! -- use mysql_result() to get the data -->
 
<?php 
$connection=mysql_connect("localhost","root","password"); //Connect and select the database server
mysql_select_db("test",$connection); 
$query="insert into users(user_name)"; //Insert a piece of data into the test database
$query.="values('tuxiaohui')"; 
$result=mysql_query($query); 
if(!$query) 
echo "insert data failed!<br>"; 
else{ 
$query="select * from users"; //Query data
$result=mysql_query($query,$connection); 
for($rows_count=0;$rows_count<7;$rows_count++) //Mysql_result () returns the contents of a cell in the MySQL result set.
{ 
echo " The user ID : ".mysql_result($result,$rows_count,"user_id")."<br>"; 
echo " User name: ".mysql_result($result,$rows_count,"user_name")."<br>"; 
} 
} 
?> 

2. < ! -- use mysql_fetch_row() to get the data and return the query results as an array -->
 
<?php 
$connection=mysql_connect("localhost","root","password"); //Connect and select the database server
mysql_select_db("test",$connection); 
$query="select * from users"; 
$result=mysql_query($query,$connection); 
while($row=mysql_fetch_row($result)) 
{ 
echo " The user ID : ".$row[0]."<br>"; 
echo " User name: ".$row[1]."<br>"; 
} 
?> 

3. < ! Mysql_fetch_array () is used to get the data, similar to mysql_fetch_row(), which gets the current row of the result set and automatically slides to the next row after the call -->
 
<?php 
$connection=mysql_connect("localhost","root","password"); //Connect and select the database server
mysql_select_db("test",$connection); 
$query="select * from users"; 
$result=mysql_query($query,$connection); 
while($row=mysql_fetch_array($result)) 
{ 
echo " The user ID : ".$row[0]."<br>"; //You can also write $row["user_id"]
echo " User name: ".$row[1]."<br>"; //You can also write $row["user_name"]
} 
?> 

4. < ! - use the mysql_fetch_object () returns the query results in the form of objects, is also used to query result set data, returns the current row data, and automatically into the next line, different is that it returns an object, the object attribute set properties for the data set, the attribute value for the current row in the database on the property on the value of the - >
 
<?php 
$connection=mysql_connect("localhost","root","root"); //Connect and select the database server
mysql_select_db("test",$connection); 
$query="select * from users"; 
$result=mysql_query($query,$connection); 
while($row=mysql_fetch_object($result)) 
{ 
echo " The user ID : ".$row->user_id."<br>"; //Through the object operator -> Gets the value of the changed row data on its properties.
echo " User name: ".$row->user_name."<br>"; 
} 
?> 

5. Comprehensive comparison:
Mysql_result () : the advantage is easy to use; Its disadvantage is that the function is less, a call can only get the result of the data set of a row of elements, the efficiency of the larger database is lower;
Mysql_fetch_row () : the advantage is that the execution efficiency is the highest of the four methods; The disadvantage is that the attribute value can only be obtained by using the number as the attribute index.
Mysql_fetch_array () : performs the same high efficiency as mysql_fetch_row(), and the bounds can be used to directly obtain the property value in the way of attribute name, so it is most commonly used in practice.
Mysql_fetch_object () : more advanced in design thinking with object-oriented thinking, if you are used to writing programs with object-oriented thinking, you will choose it. Secondly, the advantage of this method is that the results of data with a more responsible structure are more logically clear.

Related articles: