MySQL connection

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); 

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

$dbhost = localhost; // mysql server host address $dbuser = ‘< span class="hl-string">root; // mysql username $dbpass = 123456; // mysql username and password $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if (! $conn ) { die(Could not connect: . mysqli_error())< span class="hl-code">; } echo database connection is successful! ; mysqli_close($conn); ?> span> span> span> div>

Connect to MySQL

< span class="hl-inlinetags"> $dbhost = localhost; // mysql server host address $dbuser = root; // mysql username $dbpass = 123456; // mysql username and password $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die(Could not connect: . mysqli_error()); } echo The database connection is successful! ; mysqli_close($conn); ?> span> span> span> div>

$dbhost = localhost‘< span class="hl-code">; // mysql server host address $dbuser = root; // mysql username $dbpass = 123456; // mysql username and password $conn = mysqli_connect($dbhost, < span class="hl-var">$dbuser, $dbpass); if< span class="hl-brackets">(! $conn ) { die(Could not connect: . mysqli_error()); } echo database connection is successful! ; mysqli_close($conn); ?> span> span> span> div>

$dbhost = localhost; < span cl ass="hl-comment">// mysql server host address $dbuser = root; // mysql username $dbpass = 123456; // mysql username and password< span class="hl-comment"> $conn = mysqli_connect($dbhost, $dbuser< span class="hl-code">, $dbpass); if(! $conn ) { die(Could not connect: . mysqli_error()); } echo The database connection is successful! ; mysqli_close($conn); ?> span> span> span> p>

Leave a Comment

Your email address will not be published.