Detailed description of mysql connection and query instances in PHP. I read a lot of tutorials on the internet and only talked about one part. if I connect to the database, I only write connection data or query only queries, the following article describes how to configure mysql connections in php and how to query data on the internet. many tutorials only show one link. if I connect to the database, I only write connection data or query only query data, the following article describes how to configure mysql connection in php and then query data instances.
Next, let's talk about connecting PHP to the MYSQL database.
PHP and mysql connection and query process
1. preparation:
Php. ini load mysql components:
Before extension = php_mysql.dll; remove
Extension_dir = "" is the path correct?
2. PHP syntax
Link PHP to mysql functions
Mysql_connect: enable the MySQL link
Mysql_select_db: open a database
@ And or die hide error and condition display
Usage: mysql_connect ("host", "user name", "password")
Mysql_select_db ("Open Database", connection identifier );
(If the connection identifier is not specifically declared, it is the last opened connection by default .)
3. how to execute an SQL statement
Mysql_query (SQL statement, connection identifier );
Note: mysql_query is used to send queries to the current database of the database server based on the connection identifier. if the connection identifier is default, it is the last opened connection by default.
Return value: a result identifier is returned after the request is successful. if the request fails, false is returned.
4. differences between the two query functions array/row
Format: mysql_fetch_row (result );
Note: mysql_fetch_row is used to save a row of query results to an array. the subscript of this array starts from 0, and each array element corresponds to a field. You can obtain all query results through a loop.
Format: mysql_fetch_array (result );
Note: The functions of mysql_fetch_array and mysql_fetch_row are basically the same, except that they can be indexed at the offset starting from 0, or by domain name. Returns all the domain values of the next row and saves them to an array. If no row exists, false is returned.
5. garbled problem
The code is as follows: |
|
Mysql_query ("set names 'gbk'"); solves Chinese garbled characters |
6. instance:
The code is as follows: |
|
// A. link to the database $ Conn = @ mysql_connect ("localhost", "database username", "database password") or die ("Database link error "); Mysql_select_db ("database name", $ conn ); Mysql_query ("set names 'utf-8'"); // use UTF-8 Chinese encoding; // B. test the database query. $ SQL = "SELECT * FROM 'table name' order by sorting a field name desc "; $ Query = mysql_query ($ SQL ); While ($ row = mysql_fetch_array ($ query )){ Print_r ($ row ); } |
This source code is original to me and is only for learning. if it is used for illegal purposes, it has nothing to do with the author.
Configure mysql connection in mysql and then query the data...