youbbs avatar

PDO Tutorial for MySQL Developers

🕣 by youbbs

PDO Tutorial for MySQL Developers http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

顺便改写了mysql.class.php 兼容原来接口

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
90
91
92
<?php

if(!defined('IN_SAESPOT')) exit('Access Denied');

class DB_MySQL  {

    var $querycount = 0;
    var $link;

    function connect($servername, $dbport, $dbusername, $dbpassword, $dbname) {
        try {
            $this->link = new PDO('mysql:host='.$servername.';dbname='.$dbname.';charset=utf8', $dbusername, $dbpassword);
        } catch (PDOException $e) {
            $this->halt("Failed to connect to MySQL: " . $e->getMessage());
            exit();
        }
    }


    function geterrdesc() {
        return (($this->link) ? $this->link->errorInfo() : mysql_error());
    }

    function geterrno() {
        return intval(($this->link) ? $this->link->getCode() : mysql_errno());
    }

    function insert_id() {
        return $this->link->lastInsertId();
    }

    function fetch_array($query, $result_type = PDO::FETCH_ASSOC) {
        return $query->fetch($result_type);
    }

    function query($sql, $type = '') {
        if($type == 'UNBUFFERED'){
            $this->link->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
        }
        $query = $this->link->prepare($sql);
        $query->execute();
        $this->querycount++;
        return $query;
    }

    function unbuffered_query($sql) {
        $query = $this->query($sql, 'UNBUFFERED');
        return $query;
    }

    function fetch_row($query) {
        return $query->fetch(PDO::FETCH_ASSOC);
    }
    
    function fetch_one_array($query) {
        $result = $this->query($query);
        $record = $this->fetch_array($result);
        return $record;
    }

    function num_rows($query) {
        return $query->fetch(PDO::FETCH_NUM);;
    }

    function free_result($query) {
        return $query;
    }

    function halt($msg ='', $sql=''){
        $message = "<html>\n<head>\n";
        $message .= "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\">\n";
        $message .= "<style type=\"text/css\">\n";
        $message .=  "body,p,pre {\n";
        $message .=  "font:12px Verdana;\n";
        $message .=  "}\n";
        $message .=  "</style>\n";
        $message .= "</head>\n";
        $message .= "<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#006699\" vlink=\"#5493B4\">\n";

        $message .= "<p>数据库出错:</p><pre><b>".htmlspecialchars($msg)."</b></pre>\n";
        $message .= "<b>Mysql error description</b>: ".htmlspecialchars($this->geterrdesc())."\n<br />";
        $message .= "<b>Mysql error number</b>: ".$this->geterrno()."\n<br />";
        $message .= "<b>Date</b>: ".date("Y-m-d @ H:i")."\n<br />";
        $message .= "<b>Script</b>: http://".$_SERVER['HTTP_HOST'].getenv("REQUEST_URI")."\n<br />";

        $message .= "</body>\n</html>";
        @header("content-Type: text/html; charset=UTF-8");
        echo $message;
        exit;
    }
}
?>

💘 相关文章

评论

共4条关于"PDO Tutorial for MySQL Developers"的评论

写一条评论

Based on Golang + fastHTTP + sdb | go1.16.5 Processed in 1ms