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