Php+mysql database Query instance, mysql instance _php tutorial

Source: Internet
Author: User

Php+mysql database Query instance, mysql instance


This paper describes the method of Php+mysql database query. Share to everyone for your reference. The implementation method is as follows:

Copy the 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 recordset rows returned
if (mysql_num_rows ($result) >0) {
If the number of rows returned is greater than 0, it begins to appear in a tabular form
echo "









"; while ($row =mysql_fetch_row ($result)) {echo " "; echo " "; echo " "; echo " "; echo " "; } echo "
". $row [0]."". $row [1]."". $row [2]."
";
}
else{
echo "Record not found! ";
}
Frees 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, the MySQL user name, the password, the selected database name, and these variables are stored in PHP variables.

2. Once communication with the MySQL database server is established, the database server is required to open a connection. All of PHP's communication with the database passes through this connection, and in order to initialize the connection, PHP provides the mysql_connect () function. This function consists of three parameters, all of which are required, namely the database server name, user name, 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 with the mysql_select_db () function.

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

5. If mysql_query ($query) succeeds, the returned result recordset will be stored in the $result variable. This result set may contain data for one or more data rows or columns, depending on the query command we are using. Depending on the results returned, we can use the mysql_fetch_row () function to process the resulting data into a single-column array, which is stored in an array of $row. The values of the fields in this array can be accessed consecutively using the standard PHP array notation. Each time the mysql_fetch_row () function is called, the next record of the result set is returned, which makes Mysql_fetch_row () very suitable 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 released, if there are no other query operations, you can use the Mysql_close () function to close the connection to the MySQL server.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/945700.html www.bkjia.com true http://www.bkjia.com/PHPjc/945700.html techarticle Php+mysql database Query instance, mysql instance This article describes the method of Php+mysql database query. Share to everyone for your reference. The implementation method is as follows: Copy code code ...

  • 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.