How to connect to MySQL database? PHP script to connect to MySQL database instance

How to connectMySQL database? PHP script connectionMySQLdatabase instance

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
hostOptional.Specifies the hostname or IP address.
usernameOptional.Specifies the MySQL username.
PasswordOptional.Specifies the MySQL password.
dbnameOptional.Specifies the default database to use.
portOptional.Specifies the port number to attempt to connect to the MySQL server.
socketOptional.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.

prompt:Using mysqli_close() is usually not necessary because open non-persistent connections are automatically closed 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);
?>

Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "How to connect to MySQL database? PHP Script to Connect to MySQL Database Instance" will help you.

Welcome to share the link of this article:https://www.chenweiliang.com/cwl-456.html

Welcome to the Telegram channel of Chen Weiliang's blog to get the latest updates!

🔔 Be the first to get the valuable "ChatGPT Content Marketing AI Tool Usage Guide" in the channel top directory! 🌟
📚 This guide contains huge value, 🌟This is a rare opportunity, don’t miss it! ⏰⌛💨
Share and like if you like!
Your sharing and likes are our continuous motivation!

 

Comment

Your email address will not be published. Required fields * Callout

scroll to top