搜索
系统检测到您的用户名不符合规范:

ThinkPHP3.0 PDO的绑定参数

浏览:813 发布日期:2019年01月29日 分类:技术分享
=========说明:===========
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->_linkID ) 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 次 )

最佳答案
评论() 相关
后面还有条评论,
评论支持使用[code][/code]标签添加代码
您需要登录后才可以评论 登录 | 立即注册
收藏
50030
积分:1129 等级:LV3
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /