Home >Backend Development >PHP Tutorial >Detailed explanation of mysql connection and query examples in PHP_PHP tutorial
I have read many tutorials on the Internet and only cover part of them. If you connect to the database, you only write the connection data or query only the query. The following article configures the mysql connection from PHP and then queries the data instance.
Let’s talk about PHP linking to MYSQL database
The whole process of linking and querying between PHP and mysql
1. Preparation:
php.ini loads mysql component:
extension=php_mysql.dll; remove
extension_dir = “ “ Is the path correct?
2. PHP syntax
PHP link mysql function
mysql_connect: Open MySQL link
mysql_select_db: Open a database
@And or die hiding errors and conditions display
Usage: mysql_connect("Host", "Username", "Password")
mysql_select_db("Open database", connection identifier);
(If you do not specifically declare the connection identifier, the default is the last connection that is opened.)
mysql_query (SQL statement, connection identifier);
mysql_query is used to send a query to the current database of the database server based on the connection identifier. If the connection identifier is default, the default is the last opened connection.
Return value: Returns a result identifier on success, returns false on failure.
Format: mysql_fetch_row(result);
Mysql_fetch_row Used to save a row of query results to an array. The array subscript starts from 0, and each array element corresponds to a field. Through looping, all query results can be obtained.
Mysql_fetch_array and mysql_fetch_row have basically the same functions, except that in addition to using offsets starting from 0 as index, it can also use domain names as index. Value returns all field values in the next row and saves them in an array, or returns false if there is no row.
6. Example:
The code is as follows
|
Copy code
|
||||
//A. Link database |
// B. Query the database and test it
http://www.bkjia.com/PHPjc/632916.html