Php + mysql database query instance

Source: Internet
Author: User
This article mainly introduces the php + mysql database query method, analyzes the database query principles and complete implementation steps, and provides targeted analysis instructions, for more information about how to query php and mysql databases, see the following example. Share it with you for your reference. The specific implementation method is as follows:

The code is as follows:

<? Php
// Database connection parameters
$ 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 the SQL statement
$ Result = mysql_query ($ query) or die ("Error in query: $ query.". mysql_error ());
// Display the number of returned record sets
If (mysql_num_rows ($ result)> 0 ){
// If the number of returned data sets is greater than 0, the data is displayed as a table.
Echo"









";While ($ row = mysql_fetch_row ($ result )){Echo" ";Echo" ";Echo" ";Echo" ";Echo" ";}Echo"
". $ Row [0]."". $ Row [1]."". $ Row [2]."
";
}
Else {
Echo "record not found! ";
}
// Release the memory occupied by the record set
Mysql_free_result ($ result );
// Close the connection to the database
Mysql_close ($ connection );
?>

The above code is analyzed as follows:

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

2. once communication is established with the MySQL database server, a connection needs to be opened on the database server. All communication between PHP and the database passes through this connection. to initialize this connection, PHP provides the mysql_connect () function. This function includes three parameters: database server name, user name, and password. If both the database server and Web server run on the same machine, you can use localhost as the server name. Mysql_connect () returns a "connection identifier", which is saved in the variable $ connection. This identifier is used to communicate with the database.

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

4. create a query and execute it. We use the mysql_query () function to implement this function.

5. if mysql_query ($ query) is successfully executed, the returned result record set is stored in the $ result variable. This result set may contain data of one or more data rows or columns, depending on the query command we use. Depending on the returned results, we can use the mysql_fetch_row () function to convert the result data into a single column array, which is saved in the $ row array. You can continuously access the field values in this array using the array symbols of standard PHP. Every time you call the mysql_fetch_row () function, the next record of the result set is returned. This feature makes mysql_fetch_row () very suitable for the while and for loops.

6. because every result set returned after a query occupies memory, we use the mysql_free_result () function to release the memory. If no other query operation is performed after the result set is released, you can use the mysql_close () function to close the connection to the MySQL server.

I hope this article will help you with php programming.

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.