我在尝试从表中检索所有记录时遇到问题,其中用户 ID 不等于 session ID,并且 X 列中的值大于 Y 列中的值。我想返回我的结果在循环中。
到目前为止我的代码是...
// Select all users that arent the current session users and have a
// higher integer in bank column than that of the credits column
$stmt = $conn->prepare('SELECT * FROM users WHERE user_id!=? AND user_bank <= user_credits');
$stmt->bindParam(1, $uid, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// Return all records in a loop
我怎样才能做到这一点?
<小时/>错误日志
[Wed May 29 21:08:49 2013] [error] [client 89.240.62.228] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''19497918' AND user_bank <= user_credits' at line 1' in ...
最佳答案
循环将类似于:
while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
print_r( $row );
}
<小时/>
从表中选择已知列被认为是良好的做法。另外,您可以使用 <>
对于不等式运算符。
SELECT user_id, user_bank, user_credits
FROM users
WHERE user_id <> ?
AND user_bank <= user_credits
关于php - 使用 where 运算符从表中获取所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16823755/