There are many ways to connect to the mysql database in php. We usually use mysqli_connect, mysql_connect, and mysqli. If you need them, refer to them.
There are many ways to connect to the mysql database in php. We usually use mysqli_connect, mysql_connect, and mysqli. If you need them, refer to them.
1. Use Mysqli to operate Mysql
Example 1. Object oriented style
The Code is as follows: |
|
$ Mysqli = new mysqli ("localhost", "my_user", "my_password", "world "); /* Check connection */ If (mysqli_connect_errno ()){ Printf ("Connect failed: % sn", mysqli_connect_error ()); Exit (); } Printf ("Host information: % sn", $ mysqli-> host_info ); /* Close connection */ $ Mysqli-> close (); ?> /** * The above is a connection with MYSQL. * Host: the IP address type of the MYSQL host is character type. * Username: MYSQL logon username type: Login type. * Passwd: the MYSQL logon password type is confirmed. * Db: the MYSQL database name type is "Ignore. * Port: the port number of the MYSQL database. */ $ SSQL = "select * from db "; /* SQL statement to be executed */ $ Query = mysqli_query ($ connect, $ sSQL ); /* $ Connect: link is the link source. $ SSQL: query refers to the SQL statement to be executed for a request. */ While ($ arr = mysqli_fetch_array ($ query )){ /* $ Query: the result of loop $ query execution */ Print_r ($ arr ); /* Enter the expected field value */ } Mysqli_free_result ($ query ); /* Release the result set */ Mysqli_close ($ connect ); /* Close the database connection */ |
2. Use Mysql to operate Mysql
In PHP, this task is completed through the mysql_connect () function.
Syntax
Mysql_connect (servername, username, password );
Example
In the following example, we store the connection in a variable ($ con) in the script for later use. If the connection fails, run the "die" section:
The Code is as follows: |
|
$ Con = mysql_connect ("localhost", "peter", "abc123 "); If (! $ Con) { Die ('could not connect: '. mysql_error ()); } // Some code ?> /** * The above is a connection with MYSQL. * Host: the IP address type of the MYSQL host is character type. * Username: MYSQL logon username type: Login type. * Passwd: the MYSQL logon password type is confirmed. * Port: the port number of the MYSQL database. */ Mysql_select_db ("db "); /* Select Database Database Name */ $ SSQL = "select * from db "; /* SQL query statement */ $ Query = mysql_query ($ sSQL ); /* $ SSQL: query refers to the SQL statement to be executed for a request. */ While ($ arr = mysql_fetch_array ($ query )){ /* $ Query: the result of loop $ query execution */ /* Enter the expected field value */ Print_r ($ arr ); } Mysql_free_result ($ query ); /* Release the result set */ |
Close connection
When the script ends, the connection is closed. To disable the connection in advance, use the mysql_close () function.
The Code is as follows: |
|
$ Con = mysql_connect ("localhost", "peter", "abc123 "); If (! $ Con) { Die ('could not connect: '. mysql_error ()); } // Some code Mysql_close ($ con ); ?> |
3. Use mysqli in OOP Mode
The Code is as follows: |
|
$ Conn = new mysqli ("host", "username", "passwd", "db", port ); /** * The above is a connection with MYSQL. * Host: the IP address type of the MYSQL host is character type. * Username: MYSQL logon username type: Login type. * Passwd: the MYSQL logon password type is confirmed. * Db: the MYSQL database name type is "Ignore. * Port: the port number of the MYSQL database. */ $ SSQL = "select * from user "; /* SQL query statement */ $ Query = $ conn-> query ($ sSQL ); /* $ SSQL: query refers to the SQL statement to be executed for a request. */ While ($ arr = $ query-> fetch_array ()){ /* $ Query: the result of loop $ query execution */ Print_r ($ arr ); } $ Query-> close (); /* Release the result set */ $ Conn-> close (); /* Close the database connection */ |