VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 实用简单的mysql数据库连接类

  1. class DB 
  2. { 
  3.  //database connection 
  4.  var $con = FALSE; 
  5.  
  6.  function DB($MYSQL_HOST=MYSQL_HOST, $MYSQL_USER=MYSQL_USER, $MYSQL_PASS=MYSQL_PASS,$MYSQL_DB=MYSQL_DB) 
  7.  { 
  8.   $this->con = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASS) or die("Could not connect to database"); 
  9.  
  10.   if ($this->con) 
  11.   { 
  12.    @mysql_select_db($MYSQL_DB, $this->con) or die ("Could not select database"); 
  13.   } 
  14.  
  15.   return $this->con; 
  16.  } 
  17.  
  18.  
  19.  function Query($sql, $tran = false) 
  20.  { 
  21.  // if (!file_exists(MYSQL_LOG)) 
  22.  // { 
  23.  //  @umask(0); 
  24.  //  @mkdir(MYSQL_LOG, 0777); 
  25.  // } 
  26.  
  27.   // ¼־ 
  28.   //$fp = @fopen(MYSQL_LOG.date("Ymd").".txt", "a"); 
  29.  
  30.   // д־ 
  31.  // @fwrite($fp, date("Y-m-d H:i:s")."|$sql "); 
  32.  // @fclose($fp); 
  33.  
  34.   $this->sql = $sql; 
  35.  
  36.   if ($tran) 
  37.   { 
  38.    $this->result = @mysql_query($this->sql) OR $this->RollBack(); 
  39.    return $this->result; 
  40.   } 
  41.   else 
  42.   { 
  43.       mysql_query("SET NAMES 'utf8'"); 
  44.    //mysql_query("SET NAMES 'gbk'"); 
  45.    $this->result = @mysql_query($this->sql); 
  46.    return $this->result; 
  47.   } 
  48.  } 
  49.  
  50.  
  51.  function RollBack() 
  52.  { 
  53.   $this->Query("ROLLBACK;"); 
  54.   die("MySQL ROLLBACK;"); 
  55.  } 
  56.  
  57.  
  58.  function NumRows($result) 
  59.  { 
  60.   $this->result = $result; 
  61.   return @mysql_num_rows($this->result); 
  62.  } 
  63.  
  64.  
  65.  function FetchRow($result) 
  66.  { 
  67.   $this->result = $result; 
  68.   return @mysql_fetch_row($this->result); 
  69.  } 
  70.  
  71.  
  72.  function FetchArray($result) 
  73.  { 
  74.   $this->result = $result; 
  75.   return @mysql_fetch_array($this->result, MYSQL_ASSOC); 
  76.  } 
  77.  function FetchArray2($result) 
  78.  { 
  79.   $this->result = $result; 
  80.   return @mysql_fetch_array($this->result, MYSQL_BOTH); 
  81.  } 
  82.   
  83.  
  84.  function FetchObject($result) 
  85.  { 
  86.   $this->result = $result; 
  87.   return @mysql_fetch_object($this->result); 
  88.  } 
  89.  
  90.  
  91.  function FreeResult($result) 
  92.  { 
  93.   $this->result = $result; 
  94.   return @mysql_free_result($this->result); 
  95.  } 
  96.  
  97.  function DataSeek($result) 
  98.  { 
  99.  //复位记录集指针 
  100.   $this->result = $result; 
  101.   return mysql_data_seek($this->result,0); 
  102.  } 
  103.   
  104.  function InsertID() 
  105.  { 
  106.   //$this->con = $con; 
  107.   return @mysql_insert_id($this->con); 
  108.  }//开源代码phpfensi.com 
  109.  function Close() 
  110.  { 
  111.   if($this->con) 
  112.   { 
  113.    @mysql_close($this->con); 
  114.   } 
  115.  } 
  116. }
  117.  

出处:http://www.phpfensi.com/php/20140912/5456.html


相关教程