PHP query MySQL Database sample code

Source: Internet
Author: User

1.<!--use mysql_result () to get the data-->

The code is as follows Copy Code

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

The code is as follows Copy Code
<?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-->

The code is as follows Copy Code
<?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-->

The code is as follows Copy Code

<?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 that it is easy to use; its disadvantage is that it can only get one row of elements in the result dataset at a time, and is less efficient for larger databases;
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 are very easy to confuse when used;
Mysql_fetch_array (): Execution efficiency is equally high, similar to Mysql_fetch_row (), The property value can be obtained directly by using attribute name method, so it is most commonly used in practical application.
Mysql_fetch_object (): Using object-oriented thinking, more advanced in design thinking, if accustomed to using object-oriented ideas to write programs, it will be very own to choose it. Second, the advantages of the method are also reflected in the more responsible data for the structure of the results, the logic is clearer.

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.