php pdo在函数返回中准备
不知怎的,我的执行语句说该对象没有成员“执行”。怎么了?
class EdlSqliteDb
{
const SQLITE_DRIVER = "sqlite:";
var $dbh;
var $qIndex = Array();
//
function EdlSqliteDb($dsn)
{
try
{
$this->dbh = new PDO(self::SQLITE_DRIVER . $dsn);
}
catch (PDOException $e)
{
echo "Error connecting: " . $e->getMessage() . ' ' . self::SQLITE_DRIVER . $dsn;
die();
}
return;
}
//
function addQ($index,$q)
{
$this->qIndex[$index] = $q;
}
//
function PrepareQ($index)
{
try
{
$stmt = $this->dbh->prepare($this->qIndex[$index]);
}
catch (PDOException $e)
{
echo "Db Prepare Error: " . $e->getMessage();
die();
}
return $stmt;
}
//
function DbExecutePrepared($index, $arrParameters)
{
$stmt = $this->PrepareQ($index);
if ($stmt->execute($arrParameters))
{
$row = $stmt->fetch();
return $row;
}
else
{
print "<p>dbquery(): database table update execute error</p>\n";
die();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很可能是由于准备工作失败造成的。
根据错误处理,PDO,可能只返回
false< /代码>
。
老实说,不完全确定此行为的定义位置......
更新: 在这里。默认行为是静默失败。您需要将
PDO::ATTR_ERRMODE
设置为PDO::ERRMODE_EXCEPTION
才能始终引发异常。This is most likely due to a failed preparation.
Depending on error handling, PDO, instead of raising an exception, may just return
false
.Not entirely sure where this behaviour is defined, to be honest....
Update: here it is. The default behaviour is to fail silently. You need to set
PDO::ATTR_ERRMODE
toPDO::ERRMODE_EXCEPTION
for exceptions to be risen at all times.添加:
之后:
可能有问题,因此
$this->PrepareQ($index);
返回 null,或者返回一个没有execute
方法的对象,< code>var_dump变量将有助于了解该变量是什么并调试此问题。Add:
After:
Probably there's something wrong with it so
$this->PrepareQ($index);
is returning null, or an object that has noexecute
method,var_dump
ing the variable will help knowing what that variable is and debugging this issue.