1.<!--use mysql_result () to get the data-->
Copy Code code as follows:
<?php
$connection =mysql_connect ("localhost", "root", "password"); Connect and select a 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"; Querying data
$result =mysql_query ($query, $connection);
For ($rows _count=0 $rows _count<7; $rows _count++)//Mysql_result get data and output, mysql_result () returns the contents of a unit in the MySQL result set.
{
echo "User id:". mysql_result ($result, $rows _count, "user_id"). " <br> ";
echo "Username:". 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-->
Copy Code code as follows:
<?php
$connection =mysql_connect ("localhost", "root", "password"); Connect and select a database server
mysql_select_db ("Test", $connection);
$query = "SELECT * from users";
$result =mysql_query ($query, $connection);
while ($row =mysql_fetch_row ($result))
{
echo "User id:". $row [0]. " <br> ";
echo "Username:". $row [1]. " <br> ";
}
?>
3.<!--use Mysql_fetch_array () to get the data, similar to Mysql_fetch_row (), to get the current row of data in the result set, and automatically slide the next line after the call-->
Copy Code code as follows:
<?php
$connection =mysql_connect ("localhost", "root", "password"); Connect and select a database server
mysql_select_db ("Test", $connection);
$query = "SELECT * from users";
$result =mysql_query ($query, $connection);
while ($row =mysql_fetch_array ($result))
{
echo "User id:". $row [0]. " <br> "; can also write $row["user_id"]
echo "Username:". $row [1]. " <br> "; can also write $row["user_name"]
}
?>
4.<!--Use Mysql_fetch_object () to return the query result as an object, to query the data result set, return the current row data, and automatically slide the next line, but it returns an object whose property collection is a collection of properties for the data. The value on the property is the value on that property for the current row in the database-->
Copy Code code as follows:
<?php
$connection =mysql_connect ("localhost", "root", "root"); Connect and select a database server
mysql_select_db ("Test", $connection);
$query = "SELECT * from users";
$result =mysql_query ($query, $connection);
while ($row =mysql_fetch_object ($result))
{
echo "User id:". $row->user_id. " <br> "; -> gets the value of the row data on its properties by using the object operator.
echo "Username:". $row->user_name. " <br> ";
}
?>
5. Comprehensive comparison:
Mysql_result (): The advantage is easy to use, its disadvantage is that less function, one call can only get the result data set of a row of elements, the larger database efficiency is low;
Mysql_fetch_row (): The advantage is that execution efficiency is the highest in 4 ways; The problem is that only numbers can be used as property indexes to get property values, which is very confusing when used;
Mysql_fetch_array (): Execution efficiency is also high, similar to Mysql_fetch_row (), and the boundary can be used to directly obtain attribute value, so the most commonly used in practical applications;
Mysql_fetch_object (): The use of object-oriented thinking, in the design of more advanced ideas, if accustomed to using object-oriented ideas to write programs, it will be very self selection. Second, the advantages of the method are also reflected in the more responsible data for the structure of the results, the logic is clearer.