php - 使用 where 运算符从表中获取所有值

标签 php mysql pdo

我在尝试从表中检索所有记录时遇到问题,其中用户 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/

相关文章:

PHP 单独的 If else 语句

mysql - 为什么我不能将 varchar 的长度设置为 1000

php - PHP PDO 语句可以接受表名或列名作为参数吗?

php - 使用 PDO 修改 MySQL 表中的整数

mysql - 尝试从外部脚本(同一服务器)连接到 WordPress 数据库时出现 500 内部服务器错误

php - 在 Symfony2 Doctrine findBy() 中保留数组的初始顺序

php - Laravel 5.4 symfony 新更新 SetTrustedProxies() 需要第二个参数

php - 给定日期的 SQL 查询日期时间

mysql - 需要以某种方式加速我的数据库或查询

php - 为什么 mysql 中的查询有效,但 php 中的查询不起作用,这只是由于特定的列