=========说明:===========
1.因为虚拟主机便宜,常年都是100元/年,所以用虚拟主机的空间,而没有用云主机空间,因为云主机贵,即使是优惠了。
2.因为这个便宜的虚拟主机只有php5.2,所以只能用ThinkPHP3.0
3.因为ThinkPHP3.0没有真的用上PDO,没有真的使用了参数绑定,所以不安全。
4.所以,修改了ThinkPHP的两个文件,以便可以使用PDO的参数绑定,增加数据库的安全。
===========ThinkPHP3.0/Lib/Core/model.class.php 添加的部分:=============
/**
* pdo 绑定参数,创建实例
* QQ:125315695
* 2019年01月29日
*/
public function statementPrepare($sql){
$sql = $this->parseSql($sql,$parse);
return $this->db->statementPrepare($sql);
}
/**
* pdo 绑定参数
* QQ:125315695
* 2019年01月29日
*/
public function statementBindParam($key, $value){
return $this->db->statementBindParam($key, $value);
}
/**
* 执行pdo query查询
* QQ:125315695
* 2019年01月29日
*/
public function statementQuery(){
return $this->db->statementQuery();
}
/**
* 执行pdo execute
* QQ:125315695
* 2019年01月29日
*/
public function statementExecute(){
return $this->db->statementExecute();
}
=========ThinkPHP3.0/Extend/Driver/Db/DbPdo.class.php 添加的部分:=============
/**
* pdo的查询,可以先绑定参数
* QQ:125315695
* 2019年01月29日
*/
public function statementPrepare($str){
$this->initConnect(false);
if ( !$this->_li
nkID ) return false;
$this->queryStr = $str;
//释放前次的查询结果
if ( !empty($this->PDOStatement) ) $this->free();
N('db_query',1);
// 记录开始执行时间
G('queryStartTime');
$this->PDOStatement = $this->_linkID->prepare($str);
if(false === $this->PDOStatement){
throw_exception($this->error());
}
}
/**
* 绑定参数
* QQ:125315695
* 2019年01月29日
*/
public function statementBindParam($key, $value){
$this->PDOStatement->bindParam($key, $value);
}
/**
* 执行pdo query查询,适合获取多行数组
* QQ:125315695
* 2019年01月29日
*/
public function statementQuery(){
if(false === $this->PDOStatement){
throw_exception($this->error());
}
$result = $this->PDOStatement->execute();
$this->debug();
if(false === $result){
$this->error();
return false;
} else {
return $this->getAll();
}
}
/**
* 执行pdo execute,适合增加、修改、删除
* QQ:125315695
* 2019年01月29日
*/
public function statementExecute(){
if(false === $this->PDOStatement){
throw_exception($this->error());
}
$result = $this->PDOStatement->execute();
$this->debug();
if ( false === $result) {
$this->error();
return false;
} else {
$this->numRows = $this->PDOStatement->rowCount();
if($flag || preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $this->queryStr)) {
$this->lastInsID = $this->getLastInsertId();
return $this->lastInsID;
}
return $this->numRows;
}
}
===========使用Pdo的例子===========
<?php
class PdoTestAction extends Action {
//pdo 查询
public function one(){
$where = " 1 ";
$where .= " AND title LIKE :title ";
$title = "中";
$field = " * ";
$sql = "SELECT {$field} FROM ".C('DB_PREFIX')."case WHERE {$where}";
M()->statementPrepare($sql); //预处理sql
M()->statementBindParam(":title", '%'.$title.'%'); //参数绑定
$select = M()->statementQuery(); //获取数组
dump($select);
}
//pdo 插入
public function two(){
$sql = "INSERT INTO ".C('DB_PREFIX')."case (title, detail) VALUES (:title, :detail)"; //返回最后id
M()->statementPrepare($sql); //预处理sql
$title = "中中中中";
$detail = "国国国国国";
M()->statementBindParam(':title', $title); //参数绑定
M()->statementBindParam(':detail', $detail); //参数绑定
$insertId = M()->statementExecute(); //执行插入一条记录
dump($insertId);
}
//pdo 更新
public function three(){
$sql = "UPDATE ".C('DB_PREFIX')."case SET title=:title, detail=:detail WHERE id=3"; //update 返回更新的条数1
M()->statementPrepare($sql); //预处理sql
$title = "非非非非";
$detail = "洲洲洲洲洲";
M()->statementBindParam(':title', $title); //参数绑定
M()->statementBindParam(':detail', $detail); //参数绑定
$update = M()->statementExecute(); //执行更新一条记录
dump($update);
}
}
附件
thinkphp3.0+Pdo.rar
( 1.67 KB 下载:2 次 )