PDO Tutorial for MySQL Developers

By youbbs at 2015-07-16 15:31:28 • 656次点击

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

顺便改写了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: text/html; charset=UTF-8");
        echo $message;
        exit;
    }
}
?>

pdo, tutorial, for, mysql, developers


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

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

lincanbin at 2015-07-17 12:34:35
1

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

youbbs at 2015-07-17 12:41:46
2

更好记号个

大主演 at 2015-08-04 17:29:07
3

@大主演 测试一下

梓煊 at 2015-08-23 23:07:09
4
登录 后发表评论