A summary of how to connect MySQL database in PHP

Source: Internet
Author: User
Tags mysql host mysql login port number

1, use mysqli operation MySQL

Example 1. Object oriented style

The code is as follows Copy Code

<?php
$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 ();
?>

/**
* Above is to establish a connection with MySQL.
* Host: For MySQL host address type is character type.
* Username: for MySQL login username type is character.
* passwd: The MySQL login password type is character type.
* DB: is a character type for the MySQL database name.
* Port: The port number for the MySQL database.
*/
$sSQL = "SELECT * from DB";
/* The SQL statement that needs to be executed * *
$query = Mysqli_query ($connect, $sSQL);
/*
$connect: Link is a source of links
$sSQL: Query also refers to the SQL statement that needs to be executed
*/
while ($arr = Mysqli_fetch_array ($query)) {
/* $query: The results of cyclic $query * *
Print_r ($arr);
/* Enter the corresponding value of the field * *
}
Mysqli_free_result ($query);
/* Release result set * *
Mysqli_close ($connect);
/* Close Database connection/*


2, use MySQL operation MySQL

In PHP, this task is done through the mysql_connect () function.

Grammar
Mysql_connect (Servername,username,password);

Example
In the following example, we store a connection in a variable ($con) in a script for later use. If the connection fails, the "Die" section is executed:

The code is as follows Copy Code

<?php
$con = mysql_connect ("localhost", "Peter", "abc123");
if (! $con)
{
Die (' Could not connect: '. Mysql_error ());
}

Some code

?>

/**
* Above is to establish a connection with MySQL.
* Host: For MySQL host address type is character type.
* Username: for MySQL login username type is character.
* passwd: The MySQL login password type is character type.
* Port: The port number for the MySQL database.
*/
mysql_select_db ("db");
/*
Select Database
DB is the database name
*/
$sSQL = "SELECT * from DB";
/*sql Query Statement * *
$query = mysql_query ($sSQL);
/*
$sSQL: Query also refers to the SQL statement that needs to be executed
*/
while ($arr = Mysql_fetch_array ($query)) {
/* $query: The results of cyclic $query * *
/* Enter the corresponding value of the field * *
Print_r ($arr);
}
Mysql_free_result ($query);
/* Release result set * *

Close connection
As soon as the script finishes, the connection closes. Use the Mysql_close () function if you want to close the connection in advance.

The code is as follows Copy Code

<?php
$con = mysql_connect ("localhost", "Peter", "abc123");
if (! $con)
{
Die (' Could not connect: '. Mysql_error ());
}

Some code

Mysql_close ($con);
?>

3. Using mysqli in OOP mode

  code is as follows copy code

   $conn = new Mysqli (" host "," username "," passwd "," DB ", port);
/* *
* above is to establish a connection with MySQL.
* Host: is a character type for the MySQL host address type.
* Username: Character type for MySQL login username.
* passwd: Character type for the MySQL login password.
* DB: is a character type for the MySQL database name.
* Port: The port number for the MySQL database.
*/
   $sSQL = "SELECT * from user";
/*sql Query statement */
   $query = $conn->query ($sSQL); /*
$sSQL: Query also refers to the SQL statement to be executed
*/
   while ($arr = $query->fetch_array ()) {
/* $query: Loop $ Results executed by query */
    print_r ($arr);
  }
   $query->close ();
/* Release result set */
   $conn->close ();
* Close Database connection */

Related Article

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.