php - 在 where 子句中找不到列

标签 php mysql

我正在使用 php,我正在尝试制作一个页面,您可以在其中查看存储在 mysql 服务器中的公共(public)上传内容。

从我一直在使用的代码开始给我一个错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'public' in 'where clause'

我正在尝试查看我的数据库中有多少行具有“公共(public)”

// Find out how many items are in the table
$p = 'public';
$total = $db->query("SELECT COUNT(*) FROM uploads WHERE world_access = $p")->fetchColumn();

不知道是php还是sql。

// Prepare the paged query
$stmt = $db->prepare('SELECT * FROM uploads WHERE world_access = :y ORDER BY upload_time DESC LIMIT :limit OFFSET :offset');
// Bind the query params
$stmt->bindParam(':y', $p, PDO::PARAM_INT);
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();

分页 Simple PHP Pagination script

最佳答案

用引号将 $p 括起来

$total = $db->query("SELECT COUNT(*) FROM uploads WHERE world_access = '$p'")->fetchColumn();

同样在您的 PDO 中,:y 是字符串吗?然后使用 PDO::PARAM_STR

关于php - 在 where 子句中找不到列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37582590/

相关文章:

php - 集合上的 Laravel block 将第一个元素作为数组返回,第二个元素作为对象返回

php - 多维表单$_POST数据键未设置

php - bigquery php 客户端无法与 Apache 网页一起使用(Ubuntu 18.10 服务器)

php - 在 3 个日期范围内搜索记录

php - 在 laravel 4 上验证上传图片

python - pymysql.err.操作错误 : 2013 with pymysql and SQLAlchemy

php - 类似对话的消息系统

php - 在多维数组中搜索任何键中的部分值

mysql - 在大表上使用 LEFT JOIN 查询真的很慢

java - SQL 查询 : Wrong syntax