youbbs
youbbs
3860 4 0

PDO Tutorial for MySQL Developers

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

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

<?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](/name/header("content-Type) : text/html; charset=UTF-8");
        echo $message;
        exit;
    }
}
?>
0

See Also

Nearby


Discussion (4)

lincanbin
lincanbin 2015-07-17 04:34

https://github.com/lincanbin/PHP-PDO-MySQL-Class

PDO的优越性在于有MySQLdb那样的参数绑定,从原理上就避免了SQL注入,你这个封装,并没有把参数绑定/预处理这个功能加进去。

0
youbbs
youbbs 2015-07-17 04:41

@lincanbin 懒得改脚本里的SQL 语句

0
大主演
大主演 2015-08-04 09:29

更好记号个

0
梓煊
梓煊 2015-08-23 15:07

@大主演 测试一下

0
Login Topics