Home >Backend Development >PHP Tutorial >Understand the specific function usage of PHP to establish and close database connections_PHP tutorial
PHP establishes and closes database connection mysql_connect()
resource mysql_connect
([string hostname
[:port][:/path/to/socket]
[,string username] [,string password]])
All parameters are optional
Example:
<ol class="dp-xml"> <li class="alt"><span><span>@mysql_connect("localhost"<br>, "user", "password") </span></span></li> <li><span>or die("Could not connect<br> to mysql server!"); </span></li> </ol>
Note that the @ symbol means that any error message caused by a failed attempt is prohibited, and the user will see It is the error message specified in die().
Note that when connecting to multiple mysql, the link ID of each connection must be specified, as follows:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute">link1</span><span> = @mysql_connect<br>("server1", "user", "password") </span></span></li> <li><span>or die("Could not connect to<br> mysql server!"); </span></li> <li class="alt"> <span>$</span><span class="attribute">link2</span><span> = @mysql_connect<br>("server2", "user", "password") </span> </li> <li><span>or die("Could not <br>connect to mysql server!"); </span></li> <li class="alt"><span> </span></li> </ol>
Mysql_pconnect() for PHP to establish and close database connections
<ol class="dp-xml"><li class="alt"><span><span>resource mysql_pconnect<br>([string hostname [:port]<br>[:/path/to/socket][,string <br>username] [,string password]]) </span></span></li></ol>
The difference from mysql_connect() is that it will first search for existing links, not Created when it exists.
Note that there is no need to explicitly close the connection (mysql_close()), because the connection will be placed in the pool, so it is called a persistent connection.
PHP establishes and closes the database connection. mysql_close()
<ol class="dp-xml"><li class="alt"><span><span>boolean mysql_close<br>([resource link_id]) </span></span></li></ol>
Closing the connection is not necessary because it can be handled by mysql's garbage collection.
If link_id is not specified, the most recent link is closed.