pdo 属性设置 ATTR_EMULATE_PREPARES =false
// 封装的方法
protected function doSql($sql,$params=array()) {
$result = null;
if (!empty($params)) { //判断是否为空
$result = $this->getLink()->prepare($sql);
foreach ($params as $key => $val) {
if (is_int($val)) {
$type = PDO::PARAM_INT;
} else if (is_bool($val)) {
$type = PDO::PARAM_BOOL;
} else if (is_null($val)) {
$type = PDO::PARAM_NULL;
} else {
$type = PDO::PARAM_STR;
}
if (is_int($key)) {
$key += 1;
}
$result->bindValue($key,$val,$type);
}
$result->execute();
} else { // 当参数为空的时候 执行mycat 就会报ArrayIndexOutOfBoundsException
//直接执行sql语句
$result = $this->getLink()->query($sql);
}
return $result;
}
// 调用上边的方法
public function test_pdo() {
$gindex = 900040;
$gindexVer = 2;
$gUid = '352e5f69b0675a19df23d3e5ec78fcaa';
$db = getWmodDb();
// 下边两行,是可以正常执行的
$sql =" SELECT id FROM t_table WHERE gindex=? and gindex_ver=? and guid=?";
$list = $db->dosql($sql, array($gindex, $gindexVer, $gUid));
// 下边两行,是会报错的。ArrayIndexOutOfBoundsException
$sql =" SELECT id FROM t_table ";
$list = $db->dosql($sql);
echo 'pdo exis value
';
dump($list);
$sqls =" SELECT id FROM wmod_change_game";
$lists = $db->vgetData($sqls);
echo 'pdo empty exis
';
dump($lists);die;
}