Php+mysql database Query Instance _php skills

Source: Internet
Author: User

The example of this article describes the method of Php+mysql database query. Share to everyone for your reference. The implementation methods are as follows:

Copy Code code as follows:
<?php
Parameters for connecting to a database
$host = "localhost";
$user = "root";
$pass = "zq19890319";
$db = "Phpdev";
Create a MySQL connection
$connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect!");
Select a database
mysql_select_db ($db) or Die ("Unable to select database!");
Start query
$query = "SELECT * from symbols";
Execute SQL statement
$result = mysql_query ($query) or Die ("Error in Query: $query.". Mysql_error ());
Shows the number of rows returned from the recordset
if (mysql_num_rows ($result) >0) {
If the number of rows returned is greater than 0, the table will begin to appear in the form
echo "<table cellpadding=10 border=1>";
while ($row =mysql_fetch_row ($result)) {
echo "<tr>";
echo "<td>". $row [0]. " </td> ";
echo "<td>". $row [1]. " </td> ";
echo "<td>". $row [2]. " </td> ";
echo "</tr>";
}
echo "</table>";
}
else{
echo "Record not found! ";
}
Frees the memory occupied by the recordset
Mysql_free_result ($result);
Close the database connection
Mysql_close ($connection);
?>

The above code analysis is as follows:

1. Establish a connection to the database server. This information includes the server address, MySQL username, password, selected database name, and these variables are stored in PHP variables.

2. Once you have established a communication with the MySQL database server, you need the database server to open a connection. All communication between PHP and the database is through this connection, and PHP provides the mysql_connect () function to initialize the connection. This function includes three parameters, which are required, namely the database server name, username, and password. If both the database server and the Web server are running on the same machine, you can use localhost as the server name. Mysql_connect () returns a "connection identifier", which is stored in the variable $connection. This identifier is used to communicate with the database.

3. When connecting to a database using $connection, you need to select a database using the mysql_select_db () function.

4. Create a query and execute, we use the mysql_query () function to implement this function.

5. If the execution of mysql_query ($query) succeeds, the resulting recordset returned will be stored in the $result variable. This result set may contain one or more data rows or columns, depending on the query command we are using. Depending on the return result, we can use the mysql_fetch_row () function to process the result data into a single-column array, which is stored in an array of $row. Field values in this array can be accessed sequentially using the standard PHP array notation. Each time the mysql_fetch_row () function is invoked, the next record of the result set is returned, which makes Mysql_fetch_row () ideal for the while and for loops.

6. Because the result set returned after each query consumes memory, we use the Mysql_free_result () function to free up memory. After the result set is freed, you can use the Mysql_close () function to shut down the connection to the MySQL server if there are no more query operations.

I hope this article will help you with your PHP program design.

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.