Home > php教程 > php手册 > 了解PHP建立和关闭数据库连接的具体函数用法

了解PHP建立和关闭数据库连接的具体函数用法

WBOY
Release: 2016-06-13 11:09:23
Original
1598 people have browsed it

PHP建立和关闭数据库连接之mysql_connect()

resource mysql_connect
([string hostname 
[:port][:/path/to/socket]
[,string username] [,string password]]) 

所有参数都是可选的

举例:
 

<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>
Copy after login

注意,@符号表示禁止失败尝试导致的任何错误信息,用户将看到的是die()中指定的错误信息.

注意,当与多个mysql进行连接时,必须指定每个连接的链接ID,如下:

<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>
Copy after login

PHP建立和关闭数据库连接之mysql_pconnect()

<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>
Copy after login

与mysql_connect()不同的是:会首先查找现有链接,不存在时才创建.
注意,不需要显示关闭连接(mysql_close()),因为连接将放在池中,所以叫持久连接.

PHP建立和关闭数据库连接之mysql_close()

<ol class="dp-xml"><li class="alt"><span><span>boolean mysql_close<br>([resource link_id]) </span></span></li></ol>
Copy after login

关闭连接不是必须的,因为可以由mysql的垃圾回收来处理.
如果没有指定link_id,则关闭最近的链接.


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template