How PHP operates the MySQL database

Source: Internet
Author: User

Use the mysql_connect () function to connect to the MySQL server

To operate the MySQL database, you must first establish a connection to the MySQL server. The following statements connect to the MySQL server:

mysql_connect (' hostname ', ' username ', ' password ');

The return value of the function represents the database connection. If the connection succeeds, the function returns a resource to prepare for executing the SQL command later.

The instance code is as follows:

<?php $link = mysql_connect ("localhost", "root", "root") or Die ("Cannot connect to the database server! May be

The database server is not started or the user name password is incorrect!   ". Mysql_error ()); Connect to MySQL server

if ($link) {

echo "Data connection succeeded!";

}

?>

Operation Result:

Use the mysql_select_db () function to select a database file

After connecting to the MySQL database server, next use the mysql_select_db () function to select the database.

The syntax format is as follows:

mysql_select_db (String database name [, Resource Link_identifier])

Or

Mysql_qurey ("Use database name" [. Resource Link_identifier]);

The instance code is as follows:

<?php
$link = mysql_connect ("localhost", "root", "root") or Die ("Cannot connect to the database server! The database server may not be started, or the user name password is incorrect!   ". Mysql_error ()); Connect to MySQL server
$db _selected=mysql_select_db ("Db_database18", $link);
$db _selected=mysql_query ("Use Db_database18", $link);
if ($db _selected) {
echo "Database selection succeeded!";
}
?>

Operation Result:

How PHP operates the MySQL database

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.