您好,欢迎访问一九零五行业门户网

[原创]php数据库连接类_PHP教程

[原创]php数据库连接类
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.=.$value[1].n;
  }
  $db->close();
  return $str;    
 }
本站原创转戴载注明 www.111cn.cn
http://www.bkjia.com/phpjc/445022.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/445022.htmltecharticle[原创]php数据库连接类 ?php class db{ public $conn; public $root='localhost'; public $user='root'; public $pass='root'; public $db='111cn.cn'; public $charset='gb2312'; publ...
其它类似信息

推荐信息