Article directory
How to connect to a MySQL database ? An example of using a PHP script to connect to a MySQL database .
MySQL connection
use mysql binary connection
You can use the MySQL binary mode to enter the mysql command prompt to connect to the MySQL database.
Examples
The following is a simple example of connecting to a mysql server from the command line:
[root@host]# mysql -u root -p Enter password:******
After a successful login, the mysql> command prompt window will appear, and you can execute any SQL statement on it.
After the above command is executed, the successful login output 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.
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 privileges are sufficient, any user can perform SQL operations in the mysql command prompt window.
To exit the mysql> command prompt window you can use the exit command as follows:
mysql> exit Bye
Connect to MySQL using a PHP script
PHP provides the mysqli_connect() function to connect to the database.
The function has 6 parameters, returns the connection ID after a successful connection to MySQL, and returns FALSE on failure.
grammar
mysqli_connect(host,username,password,dbname,port,socket);
Parameter Description:
| 参数 | Description |
|---|---|
| host | Optional.Specifies the hostname or IP address. |
| username | Optional.Specifies the MySQL username. |
| Password | Optional.Specifies the MySQL password. |
| dbname | Optional.Specifies the default database to use. |
| port | Optional.Specifies the port number to attempt to connect to the MySQL server. |
| socket | Optional.Specifies the socket or named pipe to use. |
You can use PHP's mysqli_close() function to disconnect from a MySQL database.
This function has only one parameter, the MySQL connection identifier returned by the mysqli_connect() function after a successful connection is created.
grammar
bool mysqli_close ( mysqli $link )
This function closes the non-persistent connection to the MySQL server associated with the specified connection ID.If no link_identifier is specified, the last open connection is closed.
Note: You usually don't need to use mysqli_close(), because any open non-persistent connections will be closed automatically after the script finishes executing.
Examples
You can try the following instance to connect to your MySQL server:
connect to MySQL
<?
php
$dbhost = 'localhost:3306'; // mysql服务器主机地址
$dbuser = 'root'; // mysql用户名
$dbpass = '123456'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('连接失败: ' . mysqli_error($conn));
}
echo '连接成功';
mysqli_select_db($conn, 'chenweiliang' );
mysqli_close($conn);
?>Hopefully, the article "How to Connect to a MySQL Database? A PHP Script Example of Connecting to a MySQL Database," shared on Chen Weiliang's blog ( https://www.chenweiliang.com/ ), will be helpful to you.
Feel free to share this article's link: https://www.chenweiliang.com/cwl-456.html
