0
我正在尝试编写“通用”函数来执行SQL查询并返回JSON并处理其中的错误。在进程中有一些我不明白的地方 - 为什么尝试catch块不处理错误。 (稍后我会改进逻辑 - 问题不在于此,而仅仅是错误处理)。尝试捕获块中的未捕获错误
这里是我的代码:
public
function sqlToJSON($query, $type = array(), $params = array())
{
try {
$data = array();
$stmt = $this->mysqli->prepare($query);
if (count($type) > 0 && count($params) > 0) {
call_user_func_array(array($stmt, "bind_param"), array_merge(array($type), $params));
}
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
$stmt->free_result();
$stmt->close();
return array('result' => 'success', 'error' => null, 'data' => $data);
} catch (Exception $e) {
$this->sqlError = 'Caught exception: ' . $e->getMessage();
return array('result' => 'failed', 'error' => $e->getMessage(), 'data' => null);
}
}
我如何调用该函数:
$q = 'INSERT INTO receivers (receiver_name, owner) VALUES (UPPER(?), ?)';
$params = array(&$receiverName, &$clientEmail);
$this->sqlToJSON($q, 'ss', $params);
这给了以下预期错误:
Uncaught Error: Call to a member function fetch_assoc() on boolean in ...
我不明白为什么在这种情况下catch块没有执行?
这不是一个例外!,这是一个致命的错误, –
@Ayaou,好的谢谢。我可以与之合作:)如何关闭我的问题? – lapkritinis
删除它,简单! – rahulsm