php封裝的串連Mysql類及用法分析_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php封裝的串連Mysql類及用法。分享給大家供大家參考,具體如下:

class mysql{private $db_name;private $db_host;private $db_user;private $db_pwd;private $conn;private $querysql;private $result;private $resultarray=array();private $row;//建立建構函式 資料庫名 主機名稱 使用者名稱 密碼function __counstruct($dbname,$dbhost,$dbuser,$dbpwd){$this->db_name=$dbname;$this->db_host=$dbhost;$this->db_pwd=$dbpwd;$this->db_user=$dbuser;$this->dbconnect();$this->selectdb();}//串連資料庫private function db_connect(){$this->conn=mysql_connect($this->db_host,$this->db_user,$this->db_pwd) or die("Could not Connect MySql Server");}private function selectdb(){mysql_select_db($this->db_name) or die("unable to select dbname")}private function query(){return $this->result=mysql_query($this->querysql);}private function get_result($sql){$this->querysql=$sql;$this->query();if($this->get_num()>0){//mysql_fetch_assoc()和 mysql_fetch_array(,MYSQL_ASSOC)從結果集中取得一行作為關聯陣列 沒有則返回falsewhile($this->rows=mysql_fetch_array($this->result)){//賦值 數組賦值 resultarray[]= 將影響的行數賦值給數組$this->resultarray[]=$this->rows}return $this->resultarray;}}//$result傳回值為 bool類型 false為沒有資料private function get_num(){return $this->num=mysql_num_rows($this->result);}}$m=new mysql("testuser","localhost","root","root");$arreresult=$m->get_result("select * from userinfo");

希望本文所述對大家php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.