MySQL connection

Use mysql binary connection

You can use the MySQL binary way down into the mysql command prompt, connect to the MySQL database.

Examples

The following is a simple example from the command line to connect mysql server:

[root@host]# mysql -u root -p
Enter password:******

After a successful login occurs mysql> command prompt, you can execute any SQL statement on it.

After the above command is executed, the login is successful output results are 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 example above, we use the root user to log in to mysql server, of course, you can also use other mysql user.

If the user sufficient permissions, any user can perform SQL operations in the mysql command prompt window.

Exit mysql> command prompt you can use the exit command, as follows:

mysql> exit
Bye

Use PHP script to connect MySQL

PHP provides the mysql_connect () function to connect to the database.

This function has five parameters, return after a successful link to MySQL connection identifier, else return FALSE.

grammar

connection mysql_connect(server,user,passwd,new_link,client_flag);

Parameter Description:

parameter description
server

Optional. It specifies that the server you want to connect.

It may include a port number, such as "hostname: port", or the path to a local socket, for example, the localhost ": / path / to / socket".

If the PHP directive mysql.default_host undefined (default), the default value is 'localhost: 3306'.

user Optional. username. The default is the name of the server process owner.
passwd Optional. password. The default is a blank password.
new_link Optional. If you use the same parameters second call mysql_connect () will not create a new connection, and returns the connection identifier is already open. New_link parameter change this behavior and mysql_connect () always open a new link, even if mysql_connect () has been called in front with the same parameters.
client_flag

Optional. client_flags parameter can be a combination of the following constants:

  • MYSQL_CLIENT_SSL - use SSL encryption
  • MYSQL_CLIENT_COMPRESS - Use compression protocol
  • MYSQL_CLIENT_IGNORE_SPACE - permissible interval after the function name
  • MYSQL_CLIENT_INTERACTIVE - allowing interactive timeout inactivity before closing the connection

You can use PHP's mysql_close () function to disconnect the link with the MySQL database.

This function takes a single parameter of mysql_connect () function to create the connection after a successful return of MySQL connection identifier.

grammar

bool mysql_close ( resource $link_identifier );

This function closes the specified link identifier associated to the MySQL server non-persistent connection. If not specified link_identifier, is closed on an open connection.

Tip: do not usually need to use mysql_close (), as non-persistent open connections are automatically closed after the script is finished.

Note: mysql_close () will not close by the mysql_pconnect () to establish a persistent connection.

Examples

You can try the following examples to connect to your MySQL server:

<html>
<head>
<meta charset="utf-8"> 
<title>Connecting MySQL Server</title>
</head>
<body>
<?php
   $dbhost = 'localhost:3306';  //mysql服务器主机地址
   $dbuser = 'guest';      //mysql用户名
   $dbpass = 'guest123';//mysql用户名密码
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   if(! $conn )
   {
     die('Could not connect: ' . mysql_error());
   }
   echo 'Connected successfully';
   mysql_close($conn);
?>
</body>
</html>