[PHP] php MySQL数据库类 →→→→→进入此内容的聊天室

来自 , 2020-01-29, 写在 PHP, 查看 151 次.
URL http://www.code666.cn/view/1a99f682
  1. <?php
  2. /*
  3.         More & Original PHP Framwork
  4.         Copyright (c) 2007 - 2008 IsMole Inc.
  5.  
  6.         $Id: Mysql.Class.php 2008-3-19 aming$
  7. */
  8.  
  9.  
  10. !defined('IN_MOOPHP') && exit('Access Denied');
  11.  
  12.  
  13. class MooMySQL
  14. {
  15.         var $queryCount = 0;
  16.         var $conn;
  17.         var $result;
  18.         var $rsType = MYSQL_ASSOC;
  19.         //note:查询时间
  20.         var $queryTimes = 0;
  21.        
  22.         /**
  23.          * 连接数据库
  24.          *
  25.          * @param string $dbhost
  26.          * @param string $dbdata
  27.          * @param string $dbuser
  28.          * @param string $dbpass
  29.          * @param blooean $dbOpenType
  30.          * @param string $dbcharset
  31.          */
  32.         function connect($dbhost = '', $dbuser = '', $dbpass = '', $dbdata = '', $dbOpenType = false ,$dbcharset = 'utf8') {
  33.                 if($dbOpenType) {
  34.                         $this->conn = mysql_pconnect($dbhost, $dbuser, $dbpass) or die(mysql_errno()." : ".mysql_error());
  35.                 } else {
  36.                         $this->conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_errno()." : ".mysql_error());
  37.                 }
  38.  
  39.                 $mysql_version = $this->getMysqlVersion();
  40.  
  41.                 if($mysql_version > '4.1') {
  42.                                 global $charset, $dbcharset;
  43.                                 $dbcharset = $dbcharset2 ? $dbcharset2 : $dbcharset;
  44.                                 $dbcharset = !$dbcharset && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8')) ? str_replace('-', '', $charset) : $dbcharset;
  45.                                 $serverset = $dbcharset ? 'character_set_connection='.$dbcharset.', character_set_results='.$dbcharset.', character_set_client=binary' : '';
  46.                                 $serverset .= $mysql_version > '5.0.1' ? ((empty($serverset) ? '' : ',').'sql_mode=\'\'') : '';
  47.                                 $serverset && mysql_query("SET $serverset", $this->conn);
  48.                 }
  49.  
  50.                 @mysql_select_db($dbdata, $this->conn);
  51.         }
  52.        
  53.         /**
  54.          * 关闭数据库连接,当您使用持续连接时该功能失效
  55.          *
  56.          */
  57.         function close() {
  58.                 return mysql_close($this->conn);
  59.         }
  60.        
  61.         /**
  62.          * 发送查询语句
  63.          *
  64.          * @param string $sql
  65.          * @param string $type
  66.          * @return blooean
  67.          */
  68.         function query($sql, $type = "ASSOC") {
  69.                 $this->rsType = $type != "ASSOC" ? ($type == "NUM" ? MYSQL_NUM : MYSQL_BOTH) : MYSQL_ASSOC;
  70.                 $this->result = mysql_query($sql, $this->conn);
  71.                 $this->queryCount++;
  72.                 if($this->result) {
  73.                         return $this->result;
  74.                 } else {
  75.                         return false;
  76.                 }
  77.         }
  78.        
  79.         /**
  80.          * 数据量比较大的情况下查询
  81.          *
  82.          * @param string $sql
  83.          * @param string $type
  84.          * @return blooean
  85.          */
  86.         function bigQuery($sql, $type = "ASSOC") {
  87.                 $this->rsType = $type != "ASSOC" ? ($type == "NUM" ? MYSQL_NUM : MYSQL_BOTH) : MYSQL_ASSOC;
  88.                 $this->result = mysql_unbuffered_query($sql, $this->conn);
  89.                 $this->queryCount++;
  90.                 if($this->result) {
  91.                         return $this->result;
  92.                 }
  93.                 else {
  94.                         return false;
  95.                 }
  96.         }
  97.        
  98.         /**
  99.          * 获取全部数据
  100.          *
  101.          * @param string $sql
  102.          * @param blooean $nocache
  103.          * @return array
  104.          */
  105.         function getAll($sql = "", $nocache = false) {
  106.                 if($sql) {
  107.                         if($nocache) {
  108.                                 $this->bigQuery($sql);
  109.                         } else {
  110.                                 $this->query($sql);
  111.                         }
  112.                 }
  113.                 $rows = array();
  114.                 while($row = mysql_fetch_array($this->result, $this->rsType)) {
  115.                         $rows[] = $row;
  116.                 }
  117.                 return $rows;
  118.         }
  119.        
  120.         /**
  121.          * 获取单行数据
  122.          *
  123.          * @param string $sql
  124.          * @return array
  125.          */
  126.         function getOne($sql = "") {
  127.                 if($sql) {
  128.                         $this->query($sql);
  129.                 }
  130.                 $rows = mysql_fetch_array($this->result, $this->rsType);
  131.                 return $rows;
  132.         }
  133.        
  134.         /**
  135.          * mysql_fetch_array
  136.          *
  137.          * @param string $sql
  138.          * @return
  139.          */
  140.         function fetchArray($query) {
  141.                 return mysql_fetch_array($query, $this->rsType);
  142.         }
  143.  
  144.         /**
  145.          * 取得上一步 INSERT 操作产生的 ID
  146.          *
  147.          * @param string $sql
  148.          * @return integer
  149.          */
  150.         function insertId($sql = "") {
  151.                 if($sql) {
  152.                         $row = $this->getOne($sql);
  153.                         return $row;
  154.                 } else {
  155.                         return mysql_insert_id($this->conn);
  156.                 }
  157.         }
  158.        
  159.         function insert($sql) {
  160.                 $this->result = $this->query($sql);
  161.                 $id = $this->insertId();
  162.                 return $id;
  163.         }
  164.        
  165.         /**
  166.          * 取得行的数目
  167.          *
  168.          * @param string $sql
  169.          * @return integer
  170.          */
  171.         function numRows($sql = "") {
  172.                 if($sql) {
  173.                         $this->query($sql);
  174.                         unset($sql);
  175.                 }
  176.                 $row = mysql_num_rows($this->result);
  177.                 return $row;
  178.         }
  179.        
  180.         /**
  181.          * 取得结果集中字段的数目
  182.          *
  183.          * @param string $sql
  184.          * @return integer
  185.          */
  186.         function numFields($sql = "") {
  187.                 if($sql) {
  188.                         $this->query($sql);
  189.                 }
  190.                 return @mysql_num_fields($this->result);
  191.         }
  192.        
  193.         /**
  194.          * 取得结果中指定字段的字段名
  195.          *
  196.          * @param string $data
  197.          * @param string $table
  198.          * @return array
  199.          */
  200.         function listFields($data, $table) {
  201.                 $row = @mysql_list_fields($data, $table, $this->conn);
  202.                 $count = mysql_num_fields($row);
  203.                 for($i = 0; $i < $count; $i++) {
  204.                         $rows[] = @mysql_field_name($row,$i);
  205.                 }
  206.                 return $rows;
  207.         }
  208.        
  209.         /**
  210.          * 列出数据库中的表
  211.          *
  212.          * @return array
  213.          */
  214.         function listTables($data) {
  215.                 $query = mysql_list_tables($data);
  216.                 $rows = array();
  217.                 while($row = @mysql_fetch_array($query)) {
  218.                         $rows[] = $row[0];
  219.                 }
  220.                 return $rows;
  221.         }
  222.        
  223.         /**
  224.          * 取得表名
  225.          *
  226.          * @param string $table_list
  227.          * @param integer $i
  228.          * @return string
  229.          */
  230.         function tableName($table_list, $i) {
  231.                 return @mysql_tablename($table_list, $i);
  232.         }
  233.        
  234.         /**
  235.          * 转义字符串用于查询
  236.          *
  237.          * @param string $char
  238.          * @return string
  239.          */
  240.         function escapeString($char) {
  241.                 if(!$char) {
  242.                         return false;
  243.                 }
  244.                 return @mysql_escape_string($char);
  245.         }
  246.        
  247.         /**
  248.          * 取得数据库版本信息
  249.          *
  250.          * @return string
  251.          */
  252.         function getMysqlVersion() {
  253.                 return mysql_get_server_info();
  254.         }
  255. }

回复 "php MySQL数据库类"

这儿你可以回复上面这条便签

captcha