php 类的写法
分类:热门新闻

php 类的写法//Db.class.phpclass Db { private $_dblink; public $result; public function __construct($host, $user, $password, $dbname, $charset = 'utf8') { try{ if($this-_dblink = mysql_connect($host, $user, $password)) { if(mysql_select_db($dbname)) { $this-query(SET NAMES . $charset . ;); } else { throw new Exception(mysql_error()); } } else { throw new Exception(mysql_error()); } } catch (Exception $e) { die($e-getMessage()); } }

[原创]php数据库连接类

public function query($sql) { return $this-result = mysql_query($sql); }

<?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);
   }
  }
?>

public function fetch() { return mysql_fetch_array($this-result); }

调用方法。

public function fetchAll() { $rs = array(); $rsAll = array(); while($rs = mysql_fetch_array($this-result)) { $rsAll[] = $rs; } return $rsAll; }

/*
  读取音讯分类
 */
 
 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;    
 }

public function __destruct() { mysql_close($this-_dblink); }}

本站原创转戴载注脚 www.111cn.net

复制PHP内容到剪贴板PHP代码://test.php$db = new Db('localhost', 'root', '', 'hent_qxoa');$db-query(SELECT * FROM qx_user ;);var_dump($db-fetchAll());

本文由威尼斯在线注册平台发布于热门新闻,转载请注明出处:php 类的写法

上一篇:系统安装好之后的主要任务是设置php+mysql的支出遭逢. 下一篇:相关的一个危险漏洞
猜你喜欢
热门排行
精彩图文