Heim >Backend-Entwicklung >PHP-Tutorial >一个功能还算完整的数据库操作类_PHP教程
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li> <span class="comment">/* </span> </li> <li class="alt"><span><span class="comment">* Author 墨龙 </span> </span></li> <li><span><span class="comment">* Time 2010年12月2日 15:50:35 </span> </span></li> <li class="alt"><span><span class="comment">*/</span><span> </span></span></li> <li><span> </span></li> <li class="alt"> <span class="vars">$db</span><span> = </span><span class="keyword">new</span><span> mysql(</span><span class="vars">$db_host</span><span>,</span><span class="vars">$db_user</span><span>,</span><span class="vars">$db_password</span><span>,</span><span class="vars">$db_table</span><span>,</span><span class="vars">$db_conn</span><span>,</span><span class="vars">$pre</span><span>,</span><span class="vars">$coding</span><span>); </span> </li> <li><span> </span></li> <li class="alt"> <span class="keyword">class</span><span> mysql{ </span> </li> <li><span> </span></li> <li class="alt"> <span> </span><span class="keyword">private</span><span> </span><span class="vars">$db_host</span><span>; </span> </li> <li> <span> </span><span class="keyword">private</span><span> </span><span class="vars">$db_user</span><span>; </span> </li> <li class="alt"> <span> </span><span class="keyword">private</span><span> </span><span class="vars">$db_password</span><span>; </span> </li> <li> <span> </span><span class="keyword">private</span><span> </span><span class="vars">$db_table</span><span>; </span> </li> <li class="alt"> <span> </span><span class="keyword">private</span><span> </span><span class="vars">$db_conn</span><span>; </span><span class="comment">//数据库连接标识; </span><span> </span> </li> <li> <span> </span><span class="keyword">private</span><span> </span><span class="vars">$result</span><span>; </span><span class="comment">//执行query命令的结果资源标识 </span><span> </span> </li> <li class="alt"> <span> </span><span class="keyword">private</span><span> </span><span class="vars">$sql</span><span>; </span><span class="comment">//sql执行语句 </span><span> </span> </li> <li> <span> </span><span class="keyword">private</span><span> </span><span class="vars">$pre</span><span>; </span><span class="comment">//数据库表前缀 </span><span> </span> </li> <li class="alt"> <span> </span><span class="keyword">private</span><span> </span><span class="vars">$coding</span><span>; </span><span class="comment">//数据库编码,GBK,UTF8,gb2312 </span><span> </span> </li> <li><span> </span></li> <li class="alt"><span> </span></li> <li> <span> </span><span class="keyword">function</span><span> __construct(</span><span class="vars">$db_host</span><span>,</span><span class="vars">$db_user</span><span>,</span><span class="vars">$db_password</span><span>,</span><span class="vars">$db_table</span><span>,</span><span class="vars">$db_conn</span><span>,</span><span class="vars">$pre</span><span>,</span><span class="vars">$coding</span><span>){ </span> </li> <li class="alt"><span> </span></li> <li> <span> </span><span class="vars">$this</span><span>->db_host = </span><span class="vars">$db_host</span><span>; </span> </li> <li class="alt"> <span> </span><span class="vars">$this</span><span>->db_user = </span><span class="vars">$db_user</span><span>; </span> </li> <li> <span> </span><span class="vars">$this</span><span>->db_password = </span><span class="vars">$db_password</span><span>; </span> </li> <li class="alt"> <span> </span><span class="vars">$this</span><span>->db_table = </span><span class="vars">$db_table</span><span>; </span> </li> <li> <span> </span><span class="vars">$this</span><span>->db_conn = </span><span class="vars">$db_conn</span><span>; </span> </li> <li class="alt"> <span> </span><span class="vars">$this</span><span>->pre = </span><span class="vars">$pre</span><span>; </span> </li> <li> <span> </span><span class="vars">$this</span><span>->coding = </span><span class="vars">$coding</span><span>; </span> </li> <li class="alt"> <span> </span><span class="vars">$this</span><span>->connect(); </span> </li> <li><span> </span></li> <li class="alt"><span> } </span></li> <li><span> </span></li> <li class="alt"> <span> </span><span class="keyword">function</span><span> connect(){ </span> </li> <li><span> </span></li> <li class="alt"> <span> </span><span class="vars">$this</span><span>->db_conn = @mysql_connect(</span><span class="vars">$this</span><span>->db_host,</span><span class="vars">$this</span><span>->db_user,</span><span class="vars">$this</span><span>->db_password) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span>(</span><span class="vars">$this</span><span>->show_error(</span><span class="string">"数据库链接错误,请检查数据库链接配置!"</span><span>)); </span> </li> <li> <span> </span><span class="keyword">if</span><span>(!mysql_select_db(</span><span class="vars">$this</span><span>->db_table,</span><span class="vars">$this</span><span>->db_conn)){ </span> </li> <li class="alt"><span> </span></li> <li> <span> </span><span class="func">echo</span><span> </span><span class="string">"没有找到数据表:"</span><span>.</span><span class="vars">$this</span><span>->db_table; </span> </li> <li class="alt"><span> } </span></li> <li> <span> mysql_select_db(</span><span class="vars">$this</span><span>->db_t <p align="left"></p> <div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/486033.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/486033.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">?php /* *Author墨龙 *Time2010年12月2日15:50:35 */ $db = new mysql( $db_host , $db_user , $db_password , $db_table , $db_conn , $pre , $coding ); class mysql{ private $db_host...</span> </div> <div class="art_confoot"></div> </span> </li> </ol>