Categories: PHP | Tags: | Views: 1,668
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class dbconn {
    var $conn = 0;
    function dbconn($dbhost,$dbuser,$dbpw,$dbname){
        $this->conn = mysql_connect($dbhost,$dbuser,$dbpw);
        !$this->conn && $this->halt("Connect to MySQL failed");
        $serverinfo = mysql_get_server_info($this->conn);
        if ($serverinfo > '4.1' && $GLOBALS['charset']) {
            mysql_query("SET character_set_connection=".$GLOBALS['charset'].",character_set_results=".$GLOBALS['charset'].",character_set_client=binary",$this->conn);
        }
        if ($serverinfo > '5.0') {
            mysql_query("SET sql_mode=''",$this->conn);
        }
        if ($dbname && !@mysql_select_db($dbname,$this->conn)) {
            $this->halt('Cannot use database');
        }
    }
    function select_db($dbname){
        if (!@mysql_select_db($dbname,$this->conn)) {
            $this->halt('Cannot use database');
        }
    }
    function server_info(){
        return mysql_get_server_info($this->conn);
    }
    function insert_id(){
        $arr = $this->fetch_array('SELECT LAST_INSERT_ID() as id');
 
        return $arr["id"];
    }
    function get_value($SQL,$offset=0,$field=0){
        $rt = $this->fetch_all($SQL);
        if (isset($rt[$offset][$field])) {
            return $rt[$offset][$field];
        }
        return false;
    }
 
    function query($SQL,$method = null,$error = true){
        $query = mysql_query($SQL,$this->conn);
        !$query && $error && $this->halt('Query Error: '.$SQL);
        return $query;
    }
    function fetch_array($SQL){
        $query = $this->query($SQL);
        return mysql_fetch_array($query);
    }
    function fetch_all($SQL) {
        $arr = array();
        $query = $this->query($SQL);
        while($data = mysql_fetch_array($query)) {
            $arr[] = $data;
        }
        return $arr;
    }
    function affected_rows(){
        return mysql_affected_rows($this->conn);
    }
    function num_rows($SQL){
        $query = $this->query($SQL);
        if (!is_bool($query)) {
            return mysql_num_rows($query);
        }
        return 0;
    }
    function num_fields($SQL){
        $query = $this->query($SQL);
        return mysql_num_fields($query);
    }
    function escape_string($str){
        return mysql_escape_string($str);
    }
    function free_result(){
        $void = func_get_args();
        foreach ($void as $query) {
            if (is_resource($query) && get_resource_type($query)==='mysql result') {
                mysql_free_result($query);
            }
        }
        unset($void);
    }
    function close(){
        $this->free_result();
        return @mysql_close($this->conn);
    }
    function halt($msg=null){
        exit($msg.'<br /><br />'.$sql.'<br /> '.mysql_error());
    }
}
$db = new dbconn("localhost:3306","root","","howard");

 

这篇文章来自 迷途知返(PWWANG.COM), 转载请注明出处。 版权说明

  1. May 28th, 2009 at 00:30
    Reply | Quote | #1

    网上也有N多写好的Php操作类,不过都不如自己写的来得实在,建议加上单独mysql_fetch_assoc();的操作,CG个人比较喜欢用string做索引来用

1 trackbacks

  1. php分页类 | 迷途知返 Pingback | 2009/05/27
;) :| :x :twisted: :roll: :oops: :o :mrgreen: :lol: :idea: :evil: :cry: :arrow: :P :D :?: :? :) :( :!: 8O 8)

你可以使用@somebody:开头, 来邮件通知somebody你回复了他的留言(用户名区分大小写).