我在这里有点困惑。我有以下代码:
class Users {
function count_matched_rows($needle, $haystack){
global $userdb;
$query = $userdb->prepare("SELECT COUNT(*) FROM `users` WHERE ? = ?");
$query->execute(array($haystack, $needle));
return $query->fetchColumn();
}
}
$users = new Users();
print_r($users->count_matched_rows("jeremyfifty9", "username"));
它打印出 0,期望值为 1。所以我将其更改为:
class Users {
function count_matched_rows($needle, $haystack){
global $userdb;
$query = $userdb->prepare("SELECT COUNT(*) FROM `users` WHERE `username` = 'jeremyfifty9'");
$query->execute(array($haystack, $needle));
return $query->fetchColumn();
}
}
$users = new Users();
print_r($users->count_matched_rows("jeremyfifty9", "username"));
按预期打印 1 。有谁知道为什么第一个代码打印 0 而第二个代码打印 1?
(顺便说一句 - 我试图让这个模拟mysql_num_rows
)