PHP持久连接数据库方法

其实,我以前写过PHP持久连接数据库的函数mysql_pconnect(),http://www.chhua.com/web-note871,只是没有做任何的测试,今天做了一个小小的测试,果然真的不错,尤其是连接远程数据库的时候,效果更是非常的明显。

下面再写一下PHP持久连接数据库存函数的应用方法(一个简单的不能再简单的MYSQL类)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql</span></a><span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$host</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$user</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$pw</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$dbname</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$code</span><span style="color: #339933">;</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</span><span style="color: #000088">$host</span><span style="color: #339933">,</span><span style="color: #000088">$user</span><span style="color: #339933">,</span><span style="color: #000088">$pw</span><span style="color: #339933">,</span><span style="color: #000088">$dbname</span><span style="color: #339933">,</span><span style="color: #000088">$code</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">host</span><span style="color: #339933">=</span><span style="color: #000088">$host</span><span style="color: #339933">;</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">user</span><span style="color: #339933">=</span><span style="color: #000088">$user</span><span style="color: #339933">;</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">pw</span><span style="color: #339933">=</span><span style="color: #000088">$pw</span><span style="color: #339933">;</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">dbname</span><span style="color: #339933">=</span><span style="color: #000088">$dbname</span><span style="color: #339933">;</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">code</span><span style="color: #339933">=</span><span style="color: #000088">$code</span><span style="color: #339933">;</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">conn</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> conn<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$conn</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_pconnect" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_pconnect</span></a><span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">host</span><span style="color: #339933">,</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">user</span><span style="color: #339933">,</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">pw</span><span style="color: #009900">)</span> or <a rel="noopener noreferrer nofollow" href="http://www.php.net/die" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">die</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"links error"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//持久连接</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_select_db" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_select_db</span></a><span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">dbname</span><span style="color: #339933">,</span><span style="color: #000088">$conn</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_query" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_query</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"SET NAMES <span style="color: #006699;font-weight: bold">{$this->code}</span>"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> query<span style="color: #009900">(</span><span style="color: #000088">$sql</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$result</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_query" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_query</span></a><span style="color: #009900">(</span><span style="color: #000088">$sql</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$result</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>

以上的方法,希望对大家有所帮助,至于测试,我就不写了,大家自己测试一下吧,如果有疑问,大家可以加入PHP进阶群(63417682)进行讨论。