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>
注意,@符号表示禁止失败尝试导致的任何错误信息,用户将看到的是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>
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>
与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>
关闭连接不是必须的,因为可以由mysql的垃圾回收来处理.
如果没有指定link_id,则关闭最近的链接.