PHP query Mysql database sample code

Source: Internet
Author: User

In php and mysql database queries, I have summarized four methods: mysql_result (): mysql_fetch_row (): mysql_fetch_array (): mysql_fetch_object (): four methods, next, let's test and see the best performance.

1. <! -- Use mysql_result () to obtain data -->

The Code is as follows: Copy code

<? 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 -->

 

The Code is as follows: Copy code
<? 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. <! -- Use mysql_fetch_array () to obtain data. Similar to mysql_fetch_row (), it also obtains the data of the current row in the result set and automatically slides down a row after calling -->

 

The Code is as follows: Copy code
<? 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_array ($ result ))
{
Echo "User ID:". $ row [0]. "<br>"; // You can also write $ row ["user_id"]
Echo "username:". $ row [1]. "<br>"; // You can also write $ row ["user_name"]
}
?>

4. <! -- Use mysql_fetch_object () to return the query result in the form of an object. It is also used to query the data result set, return the data of the current row, and automatically slide down the row. The difference is that it returns an object, the property set of this object is the property set of data, and the value of the property is the value of this property in the current row of the database -->

The Code is as follows: Copy code

 

<? Php
$ 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 ))
{
Echo "User ID:". $ row-> user_id. "<br>"; // use the object operator-> to obtain the value of the modified row data on its attributes.
Echo "username:". $ row-> user_name. "<br> ";
}
?>

5. comprehensive comparison:


Mysql_result (): The advantage lies in ease of use; its disadvantage is that it has fewer functions. One call can only obtain one row of elements in the result dataset, which is less efficient for larger 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, which are prone to confusion during use;
Mysql_fetch_array (): The execution efficiency is the same as that of mysql_fetch_row (), and attribute values can be directly obtained using attribute names. Therefore, this is the most common method in practical applications;
Mysql_fetch_object (): it adopts the object-oriented idea and is more advanced in design ideas. If you are used to writing programs with object-oriented thinking, you will choose it from your own. Secondly, the advantages of this method are also reflected in the logic clarity of the data results with a more responsible structure.

Related Article

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.