蒋金阳 发表于 2009-5-27 02:07:09

php数据库的连接的类

&lt;?php<br />class mysqli_class{<br />//设置数据库的主机,用户名,密码,数据名<br />private $host;<br />private $user;<br />private $pass;<br />private $conn;<br />private $date_db;<br /><br />//类的构造函数<br />function __construct($host,$user,$pass,$date_db){<br />$this-&gt;host=$host;<br />$this-&gt;user=$user;<br />$this-&gt;pass=$pass;<br />$this-&gt;conn=$this-&gt;connect();<br />$this-&gt;date_db=$date_db;<br />//$this-&gt;connect();<br />}<br />//数据库的连接函数<br />function connect(){<br />$conn=@mysqli_connect($this-&gt;host,$this-&gt;user,$this-&gt;pass,$this-&gt;date_db) or die (&quot;连接错误,参数不正确&quot;);<br />return $conn;<br />}<br /><br />//返回数据库连接错误信息的函数<br />/*<br />function error(){<br />return mysqli_connect_error();<br />}<br />*/<br />//查询数据记录的函数<br />function query($v){<br /> $rs=mysqli_query($this-&gt;connect(),$v);<br />return $rs;<br />}<br />//插入新记录的函数<br />function fn_insert($table,$name,$value){<br />mysqli_query($this-&gt;connect(),&quot;insert into {$table} ({$name}) values ({$value})&quot;);<br />}<br />//将查询的记录显示出来<br />function show($rs){<br />    while($row=mysqli_fetch_assoc($rs)){    <br />      foreach($row as $k=&gt;$v){<br />            echo $k.&quot; -&gt; &quot;.$v.&quot;&lt;br/&gt;&quot;;<br />      }<br />      echo &quot;&lt;hr&gt;&quot;;<br />            }<br />}<br />//将查询记录返回成一个字符串<br />function showreturn($rs){<br />    while($row=mysqli_fetch_assoc($rs)){    <br />      foreach($row as $k=&gt;$v){<br />            $sz.=$k.&quot; -&gt; &quot;.$v.&quot;,&quot;;<br />      }<br />      $sz.=&quot;&lt;hr&gt;&quot;;<br />      }<br />      return $sz;<br />}<br />//将字符串拆成数组<br />function fn_sz($sz){<br />$newsz = explode(&quot;,&quot;, $sz);<br />return $newsz;<br />}<br /><br />}<br /><br /><br />?&gt;<br /><br /><blockquote class="blockquote">From: http://www.husc.cn/read.php?tid=102Powered by PHPWind.com</blockquote>
页: [1]
查看完整版本: php数据库的连接的类