Analysis of Four query return results in PHP Development

Source: Internet
Author: User

1. <! -- Use mysql_result () to obtain data -->
CopyCode The Code is as follows: <? PHP
$ Connection = mysql_connect ("localhost", "root", "password"); // connect to and select the Database Server
Mysql_select_db ("test", $ connection );
$ Query = "insert into users (user_name)"; // insert a data entry 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 ++) // use mysql_result to obtain and output data. mysql_result () returns the content 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 obtain data and return the query result in the form of an array -->
Copy codeThe Code is as follows: <? PHP
$ Connection = mysql_connect ("localhost", "root", "password"); // connect to 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 "User ID:". $ row [0]. "<br> ";
Echo "username:". $ row [1]. "<br> ";
}
?>

3.
copy Code the code is as follows: $ connection = mysql_connect ("localhost", "root", "password"); // connect to 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)
{< br> echo "User ID :". $ row [0]. "
"; // You can also write $ row ["user_id"]
echo "username :". $ row [1]. "
"; // You can also write $ row ["user_name"]
}< BR >?>

4.
copy Code the code is as follows: $ connection = mysql_connect ("localhost", "root", "root"); // connect to 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)
{< br> echo "User ID :". $ row-> user_id. "
"; // use the object operator-> to obtain the value of the modified data on its attributes.
echo "username:". $ row-> user_name. "
";
}< BR >?>

5. comprehensive comparison:
mysql_result (): it is easy to use. Its disadvantage is that it has fewer functions. One call can only obtain one row of elements in the result dataset, it is less efficient for large databases.
mysql_fetch_row (): The execution efficiency is the highest among the four methods. The disadvantage is that you can only use numbers as attribute indexes to obtain attribute values, obfuscation is very easy to use.
mysql_fetch_array (): The execution efficiency is the same as that of mysql_fetch_row (), and attribute values can be directly obtained by attribute names, therefore, it is most commonly used in practical applications.
mysql_fetch_object (): it adopts the object-oriented idea and is more advanced in terms of design ideas, if you are used to writing Program with object-oriented thinking, you can select it from your own perspective. Secondly, the advantages of this method are also reflected in the logic clarity of the data results with a more responsible structure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.