This article mainly introduces the commands for MySQL to start the connection and the basic syntax for connecting to the PHP program. it briefly describes how to call MySQL in PHP. For more information, see
Start the connection in mysql binary mode
You can use the MySQL binary method to enter the mysql command prompt and connect to the MySQL database.
Instance
The following is a simple example of connecting to the mysql server from the command line:
[root@host]# mysql -u root -p
Enter password:******
After successful logon, the mysql> command prompt window appears. you can execute any SQL statements on it.
After the preceding command is executed, the result of successful logon is as follows:
Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2854760 to server version: 5.0.9Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
In the above example, we use the root user to log on to the mysql server. of course, you can also use other mysql users to log on.
If you have sufficient permissions, any user can perform SQL operations in the mysql command prompt window.
Exit mysql> the command prompt window can use the exit command, as shown below:
mysql> exit
Bye
Use a PHP script to connect to MySQL
PHP provides the mysql_connect () function to connect to the database.
The function has five parameters. after successfully linking to MySQL, the connection id is returned. if the function fails, FALSE is returned.
Syntax
connection mysql_connect(server,user,passwd,new_link,client_flag);
Parameter description:
You can use the mysql_close () function of PHP to disconnect from the MySQL database.
This function only has one parameter that is the MySQL connection identifier returned after the mysql_connect () function is successfully created.
Syntax
bool mysql_close ( resource $link_identifier );
This function disables the non-persistent connection associated with the specified connection id to the MySQL server. If link_identifier is not specified, the last opened connection is closed.
Note: You do not need to use mysql_close () because a non-persistent connection is automatically closed after the script is executed.
Note: mysql_close () does not close persistent connections established by mysql_pconnect.
Instance
You can try the following instances to connect to your MySQL server:
Connecting MySQL Server<? Php $ dbhost = 'localhost: 808080'; // 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 'connectedsuccessfully'; mysql_close ($ conn);?>
PHP calls MySQL functions
MySQL can be used in a variety of languages, including PERL, C, C ++, JAVA, and PHP. Among these languages, Mysql is the most widely used in PHP web development.
In this tutorial, most of our instances use the PHP language. If you want to know the application of Mysql in PHP, you can visit our PHP to use Mysql introduction.
PHP provides multiple methods to access and operate Mysql database records. The format of PHP Mysql functions is as follows:
Mysql_function (value, value,...); the function section in the preceding format describes the functions of mysql functions, as shown in
- Mysqli_connect ($ connect );
- Mysqli_query ($ connect, "SQL statement ");
- Mysql_fetch_array ()
- Mysql_connect (), mysql_close ()
The following example shows the syntax for PHP to call mysql functions:
PHP with MySQL<?php $retval = mysql_function(value, [value,...]); if( !$retval ) { die ( "Error: a related error message" ); } // Otherwise MySQL or PHP Statements?>