国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

mysql數據庫連接類

2024-09-04 11:44:18
字體:
來源:轉載
供稿:網友

本款php連接mysql數據庫連接程序代碼是一款比較簡單實用的連接代碼,希望本教程對各位同學會有所幫助,代碼如下:

  1. class mysql { 
  2.       public $sqlserver = 'localhost'
  3.       public $sqluser = 'root'
  4.       public $sqlpassword = ''
  5.       public $database
  6.       public $last_query = ''
  7.       private $connection
  8.       private $query_result
  9.       public function __construct() {} 
  10.       public function __destruct() { 
  11.        $this->close(); 
  12.       } 
  13.     //+======================================================+ 
  14.     // create a connection to the mysql database 
  15.     //+======================================================+ 
  16.     public function connect($server = null, $user = null, $password = null, $database = null){ 
  17.      if (isset($server)) $this->sqlserver = $server
  18.      if (isset($user)) $this->sqluser = $user
  19.      if (isset($password)) $this->sqlpassword = $password
  20.      if (isset($database)) $this->database = $database
  21.     $this->connection = mysql_connect($this->sqlserver, $this->sqluser, $this->sqlpassword); 
  22.     if($this->connection){ 
  23.     if (mysql_select_db($this->database)){ 
  24.     return $this->connection; 
  25.     }else
  26.     return $this->error(); 
  27.     } 
  28.     }else
  29.     return $this->error(); 
  30.     } 
  31.     } 
  32.     //+======================================================+ 
  33.     // execute a query 
  34.     //+======================================================+ 
  35.     public function query($query$die = false){ 
  36.       if ($query != null){ 
  37.        $this->last_query = $query
  38.        $this->query_result = mysql_query($query$this->connection); 
  39.        if(!$this->query_result){ 
  40.         if ($diedie("die: ".$this->query_result); 
  41.         return $this->error(); 
  42.        }else
  43.         if ($diedie("die: ".$this->query_result); 
  44.         return $this->query_result; 
  45.        } 
  46.       }else
  47.        echo "empty query cannot be executed!"
  48.       } 
  49.     } 
  50.     //+======================================================+ 
  51.     // returns the result 
  52.     //+======================================================+ 
  53.     public function getresult(){ 
  54.        return $this->query_result; 
  55.     } 
  56.     //+======================================================+ 
  57.     // returns the connection 
  58.     //+======================================================+ 
  59.     public function getconnection(){ 
  60.        return $this->connection; 
  61.     } 
  62.     //+======================================================+ 
  63.     // returns an object with properties rep 
  64.     //     resenting the result fields www.111cn.net and values 
  65.     //+======================================================+ 
  66.     public function getobject($qry = null){ 
  67.      if (isset($qry)) $this->query($qry); 
  68.      return mysql_fetch_object($this->getresult()); 
  69.     } 
  70.     //+======================================================+ 
  71.     // returns an array with keys representi 
  72.     //     ng the result fields and values 
  73.     //+======================================================+ 
  74.     public function getarray($query_id = ""){ 
  75.       if($query_id == null){ 
  76.        $return = mysql_fetch_array($this->getresult()); 
  77.       }else
  78.        $return = mysql_fetch_array($query_id); 
  79.       } 
  80.       return $return ? $return : $this->error(); 
  81.     } 
  82.     //+======================================================+ 
  83.     // returns the number of rows in the res 
  84.     //     ult 
  85.     //+======================================================+ 
  86.       public function getnumrows($qry = null){ 
  87.        if (isset($qry)) $this->query($qry); 
  88.        $amount = mysql_num_rows($this->getresult()); 
  89.        return emptyempty($amount) ? 0 : $amount
  90.     } 
  91.     //+======================================================+ 
  92.     // returns if the result contains rows 
  93.     //+======================================================+ 
  94.     public function hasresults($qry = null) { 
  95.       if (isset($qry)) $this->query($qry); 
  96.      return $this->getnumrows($qry) > 0; 
  97.     } 
  98.     //+======================================================+ 
  99.     // returns the number of rows that where 
  100.     //     affected by the last action 
  101.     //+======================================================+ 
  102.     public function getaffectedrows($qry = null, $query_id = null){ 
  103.       if (isset($qry)) $this->query($qry); 
  104.       if(emptyempty($query_id)){ 
  105.        $return = mysql_affected_rows($this->getresult()); 
  106.       }else
  107.        $return = mysql_affected_rows($query_id); 
  108.       } 
  109.       return $return ? $return : $this->error(); 
  110.     } 
  111.     //+======================================================+ 
  112.     // returns the auto generated id from th 
  113.     //     e last insert action 
  114.     //+======================================================+ 
  115.     public function getinsertid($connection_link = null){ 
  116.     return mysql_insert_id(isset($connection_link) ? $connection_link : $this->connection); 
  117.     } 
  118.     //+======================================================+ 
  119.     // close the connection to the mysql dat 
  120.     //     abase 
  121.     //+======================================================+ 
  122.     public function close(){ 
  123.     if(isset($this->connection)){ 
  124.     return @mysql_close($this->connection); 
  125.     } 
  126.     else { 
  127.      return $this->error(); 
  128.     } 
  129.     } 
  130.     //+======================================================+ 
  131.     // outputs the mysql error 
  132.     //+======================================================+ 
  133.     private function error(){ 
  134.     if(mysql_error() != ''){ 
  135.     echo '<b>mysql errorwww.111cn.net</b>: '.mysql_error().'<br/>'
  136.     } 
  137.     } 
  138.     } 
  139.     //demo 
  140.     // database object initialization 
  141. $db = new mysql(); 
  142. $db->connect("localhost""root""123456""user"); 
  143. // update query 
  144. //$db->query("update table_name set field_name = value where another_field = another_value"); 
  145. // select with check for record amount 
  146. if ($db->hasresults("select * from userinfo")) { 
  147. //   loop through the user records, and get them as objects 
  148. //   note that the getobject method will use the last executed query when not provided with a new one 
  149.   while ($user = $db->getobject()) { 
  150.     echo "user $user->username is called $user->password<br /> "
  151.   }//開源代碼Vevb.com 
  152. else { 
  153.   echo "no results where found"
  154. }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兴化市| 观塘区| 鄂伦春自治旗| 宜都市| 宜宾市| 十堰市| 宁海县| 巨鹿县| 台北市| 冷水江市| 乌鲁木齐县| 青河县| 牡丹江市| 蒙阴县| 方山县| 吉木萨尔县| 台南县| 北安市| 扎囊县| 富裕县| 辽阳市| 贵德县| 京山县| 教育| 澎湖县| 长治市| 四子王旗| 张掖市| 韩城市| 河池市| 吉林市| 莱阳市| 文山县| 台前县| 通化县| 邳州市| 资中县| 延长县| 桐城市| 敦煌市| 周宁县|