mysql_connect database connection

 PHP with MySQL database can be said to be a very good choice. To open a connection with MySQL, you can use mysql_connect to establish it. You must first prepare the database related information to be connected, such as MySQL location, account, password, etc., and confirm MySQL is operating and open connection.


mysql_connect syntax structure

mysql_connect (database location path, login account, login password, new_link, client_flags);

If you are installing the Appserv package software, the MySQL database location path can be written as localhost or 127.0.0.1. If you are using a leased virtual host or other host, please prepare the location path yourself. The login account is defined by mysql.default_user by default , The login password is defined by mysql.default_password by default . Both mysql.default_user and mysql.default_password are set by php.ini . If you have a MySQL login account and password, please use them directly.

The last two items, new_link and client_flags, are optional items, which are only available since PHP4.2 and PHP4.3 respectively.

mysql_connect database connection example
<?php
$Link = mysql_connect('localhost','UserName','Password');
if (!$Link) {
 die(' Connection failed, output error message: '. mysql_error());
}
echo' connect Line success';
mysql_close($Link);
?>
The first line of the example establishes a database connection through mysql_connect. The database location is local, so fill in localhost, and then fill in the account and password. If the connection is successfully established, a MySQL link ID will be returned. If it fails, then Return false. "Die(' connection failed, output error message:'. mysql_error());" means exit and output a failed error message. The mysql_error function is used to display the error message, and the mysql_close function is used to close the MySQL database connection. line.

Post a Comment

0 Comments