MySQ connection

1. Use mysql binary mode to connect

You can use the 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 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

 2. Use PHP script to connect to MySQL

PHP provides the mysql_connect() function to connect to the database.
The function has 5 parameters, returns the connection ID after a successful connection to MySQL, and returns FALSE on failure.
grammar

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

 Parameter Description:

Parameter Description
server

Optional. Specifies the server to connect to.

Can include the port number, such as "hostname:port", or the path to the local socket, such as ":/path/to/socket" for localhost.

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

user Optional. username. The default is the username of the server process owner.
passwd Optional. password. The default value is an empty password.
new_link Optional. If mysql_connect() is called a second time with the same parameters, a new connection will not be established, but the ID of the already opened connection will be returned. The parameter new_link changes this behavior and makes mysql_connect() always open a new connection, even when mysql_connect() has been previously called with the same parameters.
client_flag

Optional. The 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 - Allow space after function name
  • MYSQL_CLIENT_INTERACTIVE - allows interactive timeout inactivity time before closing the connection

You can use PHP's mysql_close() function to disconnect from a MySQL database.

This function has only one parameter is the MySQL connection identifier returned by the mysql_connect() function after the connection is successfully created.

grammar

bool mysql_close ( resource $link_identifier );

 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.
Tip: Using mysql_close() is usually not necessary because open non-persistent connections are automatically closed after the script finishes executing.
Note: mysql_close() does not close persistent connections established by mysql_pconnect().

 

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

 

<html>
<head>
<title>Connecting MySQL Server</title>
</head>
<body>
<?php
   $dbhost = 'localhost:3306'; //mysql server host address
   $dbuser = 'guest'; //mysql username
   $dbpass = 'guest123';//mysql username and password
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   if(! $conn )
   {
     die('Could not connect: ' . mysql_error());
   }
   echo 'Connected successfully';
   mysql_close($conn);
?>
</body>
</html>

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326930688&siteId=291194637