You must say:"Yes" or "All right"! 注册 | 登陆

出自DISCUZ的PHP MYSQL操作类


PHP代码
  1. <?php   
  2.     
  3. //include("config.php");   
  4. define('db_host','localhost'); //数据库服务器   
  5. define('db_user','root');      //数据库用户名   
  6. define('dbpw','');    //数据库密码   
  7. define('dbname','test');  //数据库名   
  8. define('dbcharset','utf8');    //数据库编码,不建议修改   
  9.     
  10. //$db = new mysql($dbhost='', $dbuser='', $dbpw='', $dbname = 'test222', $pconnect = 0);   
  11.     
  12. /**  
  13. include("inc/mysql.class.php");  
  14.    
  15. $db = new mysql();  
  16.    
  17. $query = $db->query("select * from test order by ID DESC");  
  18.    
  19. //取一条记录  
  20. $res = $db->fetch_row($query);  
  21.    
  22. //取所有记录  
  23. $res = $db->fetch_all($query);  
  24.    
  25. //插入  
  26. $db->query("insert into test values (3,'测试222222222','test','fffff')");  
  27.    
  28.    
  29. //mysql_result 只取一个时  
  30. $query = $db->query("select title from test order by ID DESC");  
  31. $rs = $db->result($query,0);  
  32. print_r($rs);  
  33.    
  34. //查询  
  35. $query = $db->query("select * from test order by ID DESC");  
  36. while($rw = $db->fetch_array($query))  
  37. {  
  38.     print_r($rw);  
  39. }  
  40.    
  41.    
  42. **/  
  43.     
  44.     
  45.     
  46. /**  
  47.  * mysql查询类  
  48.  * 改造自discuz的mysql查询类  
  49.  */  
  50. class mysql {   
  51.     /**  
  52.      * 查询总次数  
  53.      *  
  54.      * @var int  
  55.      */  
  56.     var $querynum = 0;   
  57.     /**  
  58.      * 连接句柄  
  59.      *  
  60.      * @var object  
  61.      */  
  62.     var $link;   
  63.     
  64.     /**  
  65.      * 定义一些默认的变量  
  66.      */  
  67.     private $dbhost = db_host;   
  68.     private $dbname = dbname;   
  69.     private $dbuser = db_user;   
  70.     private $dbpw = dbpw;   
  71.     private $dbcharset = dbcharset;   
  72.     
  73.     /**  
  74.      * 构造函数  
  75.      *  
  76.      * @param string $dbhost 主机名  
  77.      * @param string $dbuser 用户  
  78.      * @param string $dbpw   密码  
  79.      * @param string $dbname 数据库名  
  80.      * @param int $pconnect 是否持续连接  
  81.      */  
  82.     function mysql($dbhost=''$dbuser=''$dbpw=''$dbname = ''$pconnect = 0) {   
  83.     
  84.         $dbhost==''?$dbhost=$this->dbhost:$dbhost;   
  85.         $dbuser==''?$dbuser=$this->dbuser:$dbuser;   
  86.         $dbpw==''?$dbpw=$this->dbpw:$dbpw;   
  87.         $dbname==''?$dbname=$this->dbname:$dbname;   
  88.     
  89.         if($pconnect) {   
  90.             if(!$this->link = @mysql_pconnect($dbhost$dbuser$dbpw)) {   
  91.                 $this->halt('Can not connect to MySQL server');   
  92.             }   
  93.         } else {   
  94.             if(!$this->link = @mysql_connect($dbhost$dbuser$dbpw)) {   
  95.                 $this->halt('Can not connect to MySQL server');   
  96.             }   
  97.         }   
  98.         if($this->version() > '4.1') {   
  99.             if($this->dbcharset) {   
  100.                 mysql_query("SET character_set_connection=$this->dbcharset, character_set_results=$this->dbcharset, character_set_client=binary"$this->link);   
  101.             }   
  102.     
  103.             if($this->version() > '5.0.1') {   
  104.                 mysql_query("SET sql_mode=''"$this->link);   
  105.             }   
  106.         }   
  107.     
  108.         if($dbname) {   
  109.             mysql_select_db($dbname$this->link);   
  110.         }   
  111.     
  112.     }   
  113.     /**  
  114.      * 选择数据库  
  115.      *  
  116.      * @param string $dbname  
  117.      * @return  
  118.      */  
  119.     function select_db($dbname) {   
  120.         return mysql_select_db($dbname$this->link);   
  121.     }   
  122.     /**  
  123.      * 取出结果集中一条记录  
  124.      *  
  125.      * @param object $query  
  126.      * @param int $result_type  
  127.      * @return array  
  128.      */  
  129.     function fetch_array($query$result_type = MYSQL_ASSOC) {   
  130.         return mysql_fetch_array($query$result_type);   
  131.     }   
  132.     
  133.     /**  
  134.      * 取出所有结果  
  135.      *  
  136.      * @param object $query  
  137.      * @param int $result_type  
  138.      * @return array  
  139.      */  
  140.     function fetch_all($query$result_type = MYSQL_ASSOC) {   
  141.         $result = array();   
  142.         $num = 0;   
  143.     
  144.         while($ret = mysql_fetch_array($query$result_type))   
  145.         {   
  146.             $result[$num++] = $ret;   
  147.         }   
  148.         return $result;   
  149.     
  150.     }   
  151.     
  152.     /**  
  153.      * 从结果集中取得一行作为枚举数组  
  154.      *  
  155.      * @param object $query  
  156.      * @return array  
  157.      */  
  158.     function fetch_row($query) {   
  159.         $query = mysql_fetch_row($query);   
  160.         return $query;   
  161.     }   
  162.     
  163.     /**  
  164.      * 返回查询结果  
  165.      *  
  166.      * @param object $query  
  167.      * @param string $row  
  168.      * @return mixed  
  169.      */  
  170.     function result($query$row) {   
  171.         $query = @mysql_result($query$row);   
  172.         return $query;   
  173.     }   
  174.     
  175.     
  176.     /**  
  177.      * 查询SQL  
  178.      *  
  179.      * @param string $sql  
  180.      * @param string $type  
  181.      * @return object  
  182.      */  
  183.     function query($sql$type = '') {   
  184.     
  185.         $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?   
  186.             'mysql_unbuffered_query' : 'mysql_query';   
  187.         if(!($query = $func($sql$this->link)) && $type != 'SILENT') {   
  188.             $this->halt('MySQL Query Error: '$sql);   
  189.         }   
  190.     
  191.         $this->querynum++;   
  192.         return $query;   
  193.     }   
  194.     /**  
  195.      * 取影响条数  
  196.      *  
  197.      * @return int  
  198.      */  
  199.     function affected_rows() {   
  200.         return mysql_affected_rows($this->link);   
  201.     }   
  202.     /**  
  203.      * 返回错误信息  
  204.      *  
  205.      * @return array  
  206.      */  
  207.     function error() {   
  208.         return (($this->link) ? mysql_error($this->link) : mysql_error());   
  209.     }   
  210.     /**  
  211.      * 返回错误代码  
  212.      *  
  213.      * @return int  
  214.      */  
  215.     function errno() {   
  216.         return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());   
  217.     }   
  218.     
  219.     /**  
  220.      * 结果条数  
  221.      *  
  222.      * @param object $query  
  223.      * @return int  
  224.      */  
  225.     function num_rows($query) {   
  226.         $query = mysql_num_rows($query);   
  227.         return $query;   
  228.     }   
  229.     /**  
  230.      * 取字段总数  
  231.      *  
  232.      * @param object $query  
  233.      * @return int  
  234.      */  
  235.     function num_fields($query) {   
  236.         return mysql_num_fields($query);   
  237.     }   
  238.     /**  
  239.      * 释放结果集  
  240.      *  
  241.      * @param object $query  
  242.      * @return bool  
  243.      */  
  244.     function free_result($query) {   
  245.         return mysql_free_result($query);   
  246.     }   
  247.     /**  
  248.      * 返回自增ID  
  249.      *  
  250.      * @return int  
  251.      */  
  252.     function insert_id() {   
  253.         return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);   
  254.     }   
  255.     
  256.     /**  
  257.      * 从结果集中取得列信息并作为对象返回  
  258.      *  
  259.      * @param object $query  
  260.      * @return object  
  261.      */  
  262.     function fetch_fields($query) {   
  263.         return mysql_fetch_field($query);   
  264.     }   
  265.     /**  
  266.      * 返回mysql版本  
  267.      *  
  268.      * @return string  
  269.      */  
  270.     function version() {   
  271.         return mysql_get_server_info($this->link);   
  272.     }   
  273.     /**  
  274.      * 关闭连接  
  275.      *  
  276.      * @return bool  
  277.      */  
  278.     function close() {   
  279.         return mysql_close($this->link);   
  280.     }   
  281.     /**  
  282.      * 输出错误信息  
  283.      *  
  284.      * @param string $message  
  285.      * @param string $sql  
  286.      */  
  287.     function halt($message = ''$sql = '') {   
  288.         echo $message . ' ' . $sql;   
  289.         exit;   
  290.     
  291.     }   
  292. }   
  293. ?>  

Tags: php, mysql

« 上一篇 | 下一篇 »

只显示10条记录相关文章

发表评论

评论内容 (必填):