MySQL connection and management
Let PHP support MySQL
PHP has a proprietary MySQL function library to operate MYSQL database.
MySQL is no longer supported by default in PHP 5 and later versions, so before running these libraries, please make sure php.ini is loaded with MySQL database support:
extension = mysql.dll
MySQL connection
mysql_connect() function Used to open a connection to the MySQL database.
Syntax:
mysql_connect(servername, username, password)
Although the above three parameters are optional, usually we need to specify them clearly.
Example of testing database connection:
<?php $conn = @mysql_connect("localhost","root","root1234"); if (!$conn){ die("连接数据库失败:" . mysql_error()); } else { echo "连接数据库成功!"; } ?>
In this example, the @ operator is used to mask the error message when the mysql_connect() function fails to connect and replace it with a custom error message. If the connection to the database fails, the error message is as follows:
Failed to connect to the database: Access denied for user 'root'@'localhost' (using password: YES)
After the PHP program is executed, the connection to the database will be automatically closed. If you want to close the database connection before execution is completed, you can use the mysql_close() function:
mysql_close( $conn );
Please note that the parameters are the corresponding connection resource variables.
Manage MySQL database
MySQL management is usually done through phpMyAdmin. For the installation of phpMyAdmin, please refer to "phpMyAdmin Installation".
After logging in to phpMyAdmin, you can perform various management operations allowed within the account permissions. phpMyAdmin adopts an HTML framework structure. The left side is the library table selection, and the right side is the main operation interface.
phpmyadmin main interface
On the main operation interface on the right, you can "browse data", "modify structure", "run SQL statements", "search", "insert data", "export" and "import" on the database table ", "table attribute change", "clear" or even "delete" and other operations. There will be some slight differences in the interface and operation of different versions.
Before managing a database, you need to understand some basic concepts such as creating libraries, permissions, tables and table attributes, and data types. To learn this knowledge please continue with the following tutorials.
Tips
Hosts that usually provide database support will also provide phpMyAdmin to facilitate users to manage their own databases.