程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP連接MYSQL數據庫通用類

PHP連接MYSQL數據庫通用類

編輯:關於PHP編程

  <?php include "mysql.php"; $db = new Dirver();  $db->DBLink($dbhost='localhost', $dbuser='root', $password='password', $dbname='name', $pconnect=0); ?>     PHP連接MYSQL數據庫類  mysql.php <?php   Class Dirver{        //連接數據庫   //link database   function DBLink($dbhost='localhost', $dbuser='root', $password='', $dbname='', $pconnect=0){     $this->LinkID[$this->Line] = ($pconnect == 1)? @mysql_pconnect($dbhost, $dbuser, $password) : @mysql_connect($dbhost, $dbuser, $password) or die("Connect to MySQL ($dbhost,$dbuser) failed");     //選擇連接數據庫     //choose to link database     @mysql_select_db($dbname, $this->LinkID[$this->Line]) or die('Cannot use database '.$dbname);     return $this->LinkID[$this->Line];   }      //查詢語句   //view qurry   function query($query,$limit='') {     $this -> nums ++;     //檢測如果有限制數據集則處理     //test if there is finite data,then function     if($limit>0){       $query = $query.' LIMIT '.$limit;     }     $this-> Lists[$this->Line][] = $query;               $querys = mysql_query($query,$this->LinkID[$this->Line]);     if(!$querys){       $this->DB_Error($query);     }     return $querys;   }      //返回數組資料   //back to array info   function fetch_array($query) {     return @mysql_fetch_array($query, MYSQL_ASSOC);   }      //返回數組資料   //back to array info   function result($query) {     return @mysql_result($query,$this->LinkID[$this->Line]);   }      //返回數組資料行   //back to row info   function rows($query) {     return $this->fetch_array($this->query($query));   }         //返回數組行   //back to numrows   function nums($query) {     return $this->num_rows($this->query($query));   }      //取得返回列的數目   //fetch the numbers backing out   function num_rows($query){     return @mysql_num_rows($query);   }      //返回單列的各字段   //return to every field of single row    function fetch_row($query) {     return @mysql_fetch_row($query);   }      //返回最後一次使用 INSERT 指令的 ID   //return to inserted ID used last time   function insert_id(){     return @mysql_insert_id($this->LinkID[$this->Line]);   }      //關閉當前數據庫連接   //close current database link   function close(){     return @mysql_close($this->LinkID[$this->Line]);   }      //檢測mysql版本   //test mysql version   function version(){     $query = @mysql_query("SELECT VERSION()",$this->LinkID[$this->Line]);     return  @mysql_result($query, 0);   }        //返回友情提示信息 更多http://www.52mvc.com   //return to kindly note   function DB_Error_t($query=''){      }   function DB_Error($query=''){     global $PHP_SELF;              //出錯語句提示       //error sentence        $errors = preg_replace("/'(.+?)'/is","&nbsp;'<font color='#8899DF'><b>//1</b></font>'&nbsp;",mysql_error());                  $charset ='';         //提示語言         //language noted         $lang = Array('This SQL Error Info!', 'Error Script:', 'Present time:',               'Http Host:', 'Server Name:', 'Server Software:',               'Host IP Address:', 'Remote User Agent:', 'Current File:',                'Current Line:', 'Line.', 'The Error number:',                'The specific Error was:', 'SQL Query :', 'Not discover whateverly SQL Sentence !');         //時間處理         //time         $nowdate = date('Y-m-d H:i A');         $errors = preg_replace("/'(.+?)'/is","'<font color='#8899DF'><b>//1</b></font>'",mysql_error());              //檢測是否有語句       //test if there is any sentence.       if($query==''){         $query = $lang[14];       }              echo "<html>   <head>   <meta http-equiv='Content-Type' content='text/html; charset=$charset'>   <title>$lang[0]</title>   </head>   <body>   </body>   </html>   <table style='BORDER-COLLAPSE: collapse;font-size:9pt;' borderColor='#a8b7c6' cellSpacing='1' width='100%' border='1' cellpadding='3' align='center'>           <tr>             <td bgColor='#F9F9F9' height='38' colspan='2'>             <font size='4' face='Arial' color='#800000'>$lang[0]</font></td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[2]</td>             <td bgColor='#F9F9F9'>$nowdate</td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[3]</td>             <td bgColor='#F9F9F9'><b>".$_SERVER['HTTP_HOST']."</b></td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[4]</td>             <td bgColor='#F9F9F9'>".$_SERVER['SERVER_NAME']."</td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[5]</td>             <td bgColor='#F9F9F9'>".$_SERVER['SERVER_SOFTWARE']."</td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[6]</td>             <td bgColor='#F9F9F9'><font color=/"#800000/">".$_SERVER['REMOTE_ADDR']."</font></td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[7]</td>             <td bgColor='#F9F9F9'><font color=/"#000080/">".$_SERVER['HTTP_USER_AGENT'].";</font></td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[11]</td>             <td bgColor='#F9F9F9'><b>".mysql_errno()."</b></td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[12]</td>             <td bgColor='#F9F9F9'>$errors</td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[13]</td>             <td bgColor='#F9F9F9'>$query</td>           </tr>         </table>           </td>         </tr>       </table>";     exit;   }   } ?>   PHP連接MYSQL數據庫類 <?php include "mysql.php"; $db = new Dirver();  $db->DBLink($dbhost='localhost', $dbuser='root', $password='password', $dbname='name', $pconnect=0); ?>     PHP連接MYSQL數據庫類  mysql.php <?php   Class Dirver{        //連接數據庫   //link database   function DBLink($dbhost='localhost', $dbuser='root', $password='', $dbname='', $pconnect=0){     $this->LinkID[$this->Line] = ($pconnect == 1)? @mysql_pconnect($dbhost, $dbuser, $password) : @mysql_connect($dbhost, $dbuser, $password) or die("Connect to MySQL ($dbhost,$dbuser) failed");     //選擇連接數據庫     //choose to link database     @mysql_select_db($dbname, $this->LinkID[$this->Line]) or die('Cannot use database '.$dbname);     return $this->LinkID[$this->Line];   }      //查詢語句   //view qurry   function query($query,$limit='') {     $this -> nums ++;     //檢測如果有限制數據集則處理     //test if there is finite data,then function     if($limit>0){       $query = $query.' LIMIT '.$limit;     }     $this-> Lists[$this->Line][] = $query;               $querys = mysql_query($query,$this->LinkID[$this->Line]);     if(!$querys){       $this->DB_Error($query);     }     return $querys;   }      //返回數組資料   //back to array info   function fetch_array($query) {     return @mysql_fetch_array($query, MYSQL_ASSOC);   }      //返回數組資料   //back to array info   function result($query) {     return @mysql_result($query,$this->LinkID[$this->Line]);   }      //返回數組資料行   //back to row info   function rows($query) {     return $this->fetch_array($this->query($query));   }         //返回數組行   //back to numrows   function nums($query) {     return $this->num_rows($this->query($query));   }      //取得返回列的數目   //fetch the numbers backing out   function num_rows($query){     return @mysql_num_rows($query);   }      //返回單列的各字段   //return to every field of single row    function fetch_row($query) {     return @mysql_fetch_row($query);   }      //返回最後一次使用 INSERT 指令的 ID   //return to inserted ID used last time   function insert_id(){     return @mysql_insert_id($this->LinkID[$this->Line]);   }      //關閉當前數據庫連接   //close current database link   function close(){     return @mysql_close($this->LinkID[$this->Line]);   }      //檢測mysql版本   //test mysql version   function version(){     $query = @mysql_query("SELECT VERSION()",$this->LinkID[$this->Line]);     return  @mysql_result($query, 0);   }        //返回友情提示信息   //return to kindly note   function DB_Error_t($query=''){      }   function DB_Error($query=''){     global $PHP_SELF;              //出錯語句提示       //error sentence        $errors = preg_replace("/'(.+?)'/is","&nbsp;'<font color='#8899DF'><b>//1</b></font>'&nbsp;",mysql_error());                  $charset ='';         //提示語言         //language noted         $lang = Array('This SQL Error Info!', 'Error Script:', 'Present time:',               'Http Host:', 'Server Name:', 'Server Software:',               'Host IP Address:', 'Remote User Agent:', 'Current File:',                'Current Line:', 'Line.', 'The Error number:',                'The specific Error was:', 'SQL Query :', 'Not discover whateverly SQL Sentence !');         //時間處理         //time         $nowdate = date('Y-m-d H:i A');         $errors = preg_replace("/'(.+?)'/is","'<font color='#8899DF'><b>//1</b></font>'",mysql_error());              //檢測是否有語句       //test if there is any sentence.       if($query==''){         $query = $lang[14];       }              echo "<html>   <head>   <meta http-equiv='Content-Type' content='text/html; charset=$charset'>   <title>$lang[0]</title>   </head>   <body>   </body>   </html>   <table style='BORDER-COLLAPSE: collapse;font-size:9pt;' borderColor='#a8b7c6' cellSpacing='1' width='100%' border='1' cellpadding='3' align='center'>           <tr>             <td bgColor='#F9F9F9' height='38' colspan='2'>             <font size='4' face='Arial' color='#800000'>$lang[0]</font></td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[2]</td>             <td bgColor='#F9F9F9'>$nowdate</td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[3]</td>             <td bgColor='#F9F9F9'><b>".$_SERVER['HTTP_HOST']."</b></td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[4]</td>             <td bgColor='#F9F9F9'>".$_SERVER['SERVER_NAME']."</td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[5]</td>             <td bgColor='#F9F9F9'>".$_SERVER['SERVER_SOFTWARE']."</td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[6]</td>             <td bgColor='#F9F9F9'><font color=/"#800000/">".$_SERVER['REMOTE_ADDR']."</font></td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[7]</td>             <td bgColor='#F9F9F9'><font color=/"#000080/">".$_SERVER['HTTP_USER_AGENT'].";</font></td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[11]</td>             <td bgColor='#F9F9F9'><b>".mysql_errno()."</b></td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[12]</td>             <td bgColor='#F9F9F9'>$errors</td>           </tr>           <tr>             <td bgColor='#F9F9F9' width='165'>             <p align='right'>$lang[13]</td>             <td bgColor='#F9F9F9'>$query</td>           </tr>         </table>           </td>         </tr>       </table>";     exit;   }   } ?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved