我是第一次使用PDO。我创建了这个方法,我得到了这个错误:
Call to a member function rowCount() on a non-object 以下是我的代码。
$db = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8", $dbuser, $dbpassword)
function doesRecordExist($query) {
global $db;
try {
$stmt = $db->query($query);
$count = $stmt->rowCount();
return $count;
} catch (PDOException $ex) {
die($ex->getMessage());
}
}你能帮帮我吗?
发布于 2013-01-07 05:10:23
为什么不使用预准备语句:另外,在计算查询语句之前,需要先执行它
$db = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8", $dbuser, $dbpassword)
function doesRecordExist($query) {
global $db;
try {
$stmt = $db->prepare($query);
$stmt->execute();
$count = $stmt->rowCount();
return $count;
} catch (PDOException $ex) {
die($ex->getMessage());
}
}发布于 2013-01-07 05:28:30
不要使用全局变量。将此参数作为对象进行传递。
$db = new PDO(....);
function doesRecordExist($query, PDO $db) {
try {
$stmt = $db->prepare($query);
$stmt->execute();
...发布于 2013-01-07 05:02:58
我忘记创建查询所引用的表。给您带来不便,我深表歉意
https://stackoverflow.com/questions/14186613
复制相似问题