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
- 有没有mysql的替代品
- SAE的数据库可以用到 php+mysql空间 吗
- youBBS是一款基于标准php+mysql 框架的超轻型论坛,可方便移植到其它
- mysql connection errors
- 从MySQL转向PostgreSQL
Nearby
- 上一篇 › OceanBase 支持跨行跨表事务的海量数据库
- 下一篇 › LeetCode 题解
https://github.com/lincanbin/PHP-PDO-MySQL-Class
PDO的优越性在于有MySQLdb那样的参数绑定,从原理上就避免了SQL注入,你这个封装,并没有把参数绑定/预处理这个功能加进去。
@lincanbin 懒得改脚本里的SQL 语句
更好记号个
@大主演 测试一下