“智能"使用PDO和Memcache的缓存系统 [英] "Smart" Caching System using PDO and Memcache
问题描述
我正在开发一个使用pdo和memcache的智能"缓存系统.但是,我陷入了这个错误.你能帮我吗?
I am working on a "smart" caching system that uses pdo and memcache. However, I am stuck at this error. Can you help me out?
我的代码:
$session = "a121fd4ztr6";
cache_query("SELECT * FROM `session` WHERE `session` = :session: LIMIT 1;", array(':session:' => $session));
// CACHE QUERY
function cache_query($sql, $params) {
global $db;
global $memcache;
$name = 'querycache-'.md5(serialize(array($sql, $params)));
$result = $memcache->get($name);
if (!$result) {
if(!($db)){
require("db.php");
}
$stmt = $db->prepare($sql);
$exec = $stmt->execute($params);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$memcache->add($name, $result);
}
return $result;
}
db.php建立与PDO的连接. Memcache连接已建立.我总是收到以下错误:
db.php sets up a connection to PDO. Memcache connection is already set up. I always get the following error:
添加错误模式属性后,我现在得到了更详细的错误:
after adding the error mode attribute, I now get a more detailed error:
严重错误:消息为"SQLSTATE [42000]"的未捕获异常"PDOException":语法错误或访问冲突:1064您的SQL语法有错误;请参见表11.检查与您的MySQL服务器版本相对应的手册,以在/chroot/home/html/session.php:170
^这是怎么回事?
推荐答案
看起来像$db->prepare()
返回错误,因此您的$stmt
变量不是真正的PDOStatement
对象.使用$db->errorInfo()
查看出了什么问题.如果您已经设置了适当的属性,也可以将其放在try-catch块中(请参见
Looks like $db->prepare()
returns an error, so your $stmt
variable is not a real PDOStatement
object. Use the $db->errorInfo()
to see what's going wrong. You can also put it to a try-catch block, if you have set appropriate attributes (see Errors and error handling).
要查看连接是否正常,还可以使用try-catch:
To see if your connection is OK, you also can use try-catch:
try {
$db = new PDO($dsn, $user, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
这篇关于“智能"使用PDO和Memcache的缓存系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!