[原創]php資料庫連接類

來源:互聯網
上載者:User

[原創]php資料庫連接類

<?php
 class Db{ 
  public $conn;
  public $root='localhost';
  public $user='root';
  public $pass='root';
  public $db='111cn.net';
  public $charset='gb2312';
  public $links='c';
  
  function __construct() {
    $this->connect();
  }
  
  function connect()
  {
   try{
    if( 'p' == $this->links )
    {
     $this->conn = mysql_pconnect($this->root,$this->user,$this->pass) or die(mysql_error());       
    }
    else
    {
     $this->conn = mysql_connect($this->root,$this->user,$this->pass) or die( mysql_error());
    }
    mysql_select_db($this->db,$this->conn); 
    mysql_query("set Names '$this->charset'");
   }catch (Exception $e){
    echo '資料庫連接失敗,請聯絡相關人員!';
    exit;
   } 
  }
  
  /*
   query
  */
  
  function query($sql)
  {
   $this->row = mysql_query( $sql,$this->conn ) or die( mysql_error());
   return $this->row;
  }
  /*
    mysql_num_rows total
  */
  function rows($row)
  {
   return mysql_num_rows( $row );
  }
  /*
   get data store array
  */
  function fetch($row,$tag=1)
  {
   if(1 == $tag )
   {
    return mysql_fetch_array( $row );
   }
   else
   {
    $array =array();    
    while( $rs = mysql_fetch_array( $row ) )
    {
     $array[] = $rs;     
    }
   }
   return $array;
  }
  
  /*
   取得剛插入的ID號
  */
  
  function insert_id()
  {
    return @mysql_insert_id($this->row);
  } 
   
    //close current database link
   function close()
   {
   return @mysql_close($this->conn);
   }
   
  
    //test mysql version
   function version()
   {
   $query = @mysql_query("SELECT VERSION()",$this->conn);
   return  @mysql_result($this->$row, 0);
   }
  }
?>

調用方法。

/*
  讀取新聞分類
 */
 
 function newOption()
 {
  $str='';
  $Db = new Db();
  $query = $Db->query("Select id,typetitle,typeupid,orderid from cn_111cn where typeupid=0 order by orderid asc");
  $row = $Db->fetch($query,0);
  foreach( $row as $_v => $value )
  {
   $str.="<option value=".$value[0].">".$value[1]."</option>\n";
  }
  $Db->close();
  return $str;    
 }

本站原創轉戴載註明 www.111cn.net

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.