【发布时间】:2014-01-28 20:45:25
【问题描述】:
我目前正在开发一个使用MySQLi 进行数据库访问的网站。目前执行数据库查询的代码如下所示:
$query = "SELECT height, color FROM llamas WHERE name = ?";
if(!$stmt = $connection->prepare($query)) {
// Error handling here.
}
$stmt->bind_param("s", $name);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($height, $color);
$stmt->fetch();
$stmt->close();
大多数这些调用的返回值返回FALSE 以指示存在问题。来自 PHP 文档:
成功返回
TRUE,失败返回FALSE。
如果我尝试检查所有这些函数调用是否为假,我会得到如下信息:
$query = "SELECT height, color FROM llamas WHERE name = ?";
if(!$stmt = $connection->prepare($query)) {
// Error handling here.
}
if(!$stmt->bind_param("s", $name)) {
// Error handling here.
}
if(!$stmt->execute()) {
// Error handling here.
}
if(!$stmt->store_result()) {
// Error handling here.
}
if(!$stmt->bind_result($height, $color)) {
// Error handling here.
}
if(!$stmt->fetch()) {
// Error handling here.
}
if(!$stmt->close()) {
// Error handling here.
}
我的问题是:我实际上需要检查哪些函数调用的返回值FALSE?有没有一种整洁的方法?
【问题讨论】:
标签: php error-handling mysqli