How to query mysql files in php: first use the "mysql_connect" function to connect to the mysql database; then select the specified mysql database through "mysql_select_db"; finally use the "mysql_query" method to query.
Recommended tutorial: "php mysql"
PHP connects to MySQL database
Connecting to the database
We use double exclamation marks!! to convert the resource handle into a Boolean value, and output 1 if it is correct, and an error message if it is incorrect. If the @ symbol is added in front, the error message will be ignored and no error message will be output.
For error message processing, we can use the mysql_error() function to output the error message:
mysql_connect('localhost','root','****') or die( 'Database connection failed, error message: '.mysql_error()); // Tips for password errors: Database connection failed, error message: Access denied for user 'root'@'localhost' (using password: YES)
die() function outputs a message and exits the current script. This function is an alias for the exit() function.
Database connection parameters can be stored as constants, so they cannot be modified at will and are safer.
It is worth noting that the constants in the brackets of mysql_connect() cannot be quoted, otherwise an error will occur.
Select the specified database
Usually there is no need to use mysql_close(), because the opened non-persistent connection will be automatically closed after the script is executed
mysql_select_db(database,connection): Select the MySQL database
Get the record set
mysql_query() function executes a MySQL query.
Output data
Release result set resources (only needs to be called when considering how much memory will be occupied when returning a large result set.)
The above is the detailed content of How to query mysql file in php. For more information, please follow other related articles on the PHP Chinese website!