我正在使用 Medoo 框架来处理我的数据库查询。它基本上是一个 PDO 包装器,我没有在他们的文档中找到如何处理错误或检查结果,有时它返回空数组,有时 FALSE 有时 0 等。
由于我不明白如何处理错误,这就是我目前正在使用 empty() 所做的,因为它可以处理 FALSE 、 0 和空数组,我认为这里没问题):
在 SELECT 上(Medoo 返回数组)
// Same as:
// SELECT username FROM accounts WHERE id=$id AND suspended=0
$select = $database->select("accounts",["username"], [
"AND" => [
"id" => $id,
"suspended" => 0
]
]);
// I have to check if Query failed also if row was not found
if (empty($select) === FALSE && count($select) > 0)
{
// It didn't FAIL
// So i get username like this:
$key = array_keys($select)[0];
$username = $select[$key]['username'];
}
else
{
// It FAILED
}
在 INSERT 上(Medoo 说它在这里返回 INSERT ID)
$insert = $database->insert("accounts", [
"username" => "$username"
]);
// Check if query didn't fail and actually inserted (affected rows i think?)
if (empty($insert) === TRUE OR $insert < 1)
{
// It Failed
}
更新时(这实际上是唯一明确的查询,它返回受影响的行)
$update = $database->update("accounts", ["brute_force[+]" => 1], ["id" => $user_id]);
if (empty($update) === TRUE OR $update < 1)
{
// It FAILED
}
// Check if query didn't fail and also affected row
我对这些感到非常困惑和不确定,以至于我很偏执,也许我应该像往常一样完全重写和使用 CodeIgniter。