Summary of methods for connecting to the MYSQL database in php

Source: Internet
Author: User
Tags mysql host
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 */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.