MySQL connection
Connect using mysql binary mode
You can use MySQL binary mode to enter the mysql command prompt to connect to the MySQL database.
Example
The following is a simple example of connecting to the mysql server from the command line:
[ [email protected]]# mysql -u root -p Enter password:***** *
< p>After the login is successful, the mysql> command prompt window will appear, and you can execute any SQL statement on it.
After the above command is executed, the output of successful login is as follows:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2854760 to server version: 5.0.9 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. span> span>
In the above example, we used the root user to log in to the mysql server. Of course, you can also use other mysql users to log in.
If the user authority is sufficient, any user can perform SQL operations in the mysql command prompt window.
Exit mysql> The exit command can be used in the command prompt window, as shown below:
mysql> exit Bye< /span>
Use PHP script to connect to MySQL
PHP provides the mysqli_connect() function to connect to the database.
This function has 6 parameters. After successfully connecting to MySQL, it returns the connection identifier, and if it fails, it returns FALSE.
Syntax
mysqli_connect(host,username,password,dbname,port,socket); span>
Parameter description:
Parameter< /th> | Description |
---|---|
host | Optional. Specifies the host name or IP address. |
username | Optional. Specifies the MySQL user name. |
password | Optional. Specifies the MySQL password. |
dbname | Optional. Specifies the database to be used by default. |
port | Optional. Specifies the port number to try to connect to the MySQL server. |
socket | Optional. Specifies the socket or named pipe to be used. |
You can use PHP’s mysqli_close() function to disconnect from the MySQL database.
This function has only one parameter, which is the MySQL connection identifier returned by the mysqli_connect() function after successfully creating a connection.
Syntax
bool mysqli_close ( mysqli $link )
This function closes the specified The non-persistent connection to the MySQL server associated with the connection identifier. If link_identifier is not specified, the last open connection is closed.
Tip:Usually, you do not need to use mysqli_close(), because the opened non-persistent connection will be automatically closed after the script is executed.
Examples
You can try the following examples to connect to your MySQL server:
Connect to MySQL
Connect to MySQL
span> p>
WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2951 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC