以下代码在共享主机上给出了这个错误,其中db中查看的表没有任何值
$query = "SELECT COUNT(*) FROM viewed WHERE memberID = :memberID";
$stmt = $db->prepare($query);
$stmt->bindParam(':memberID', $uid, PDO::PARAM_INT);
$stmt->execute();
$total = $stmt->fetchColumn();
$pages = ceil($total / $per_page);
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default' => 1,
'min_range' => 1,
),
)));
$offset = ($page - 1) * $per_page;
$query = "SELECT * FROM viewed WHERE memberID = :memberID ORDER BY id DESC LIMIT :per_page OFFSET :offset";
$stmt = $db->prepare($query);
$stmt->bindParam(':per_page', $per_page, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->bindParam(':memberID', $uid, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll();
但是我的配置文件是我删除
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
然后错误消失,但没有PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION我怎么知道错误?
感谢
答案 0 :(得分:0)
这种情况正在发生,因为您不需要在限制和偏移量中绑定变量。你应该只用于绑定where条件。你可以直接进行查询。见下文:
$query = "SELECT * FROM viewed WHERE memberID = :memberID ORDER BY id DESC LIMIT $per_page OFFSET $offset";
$stmt = $db->prepare($query);
$stmt->bindParam(':memberID', $uid, PDO::PARAM_INT);
答案 1 :(得分:0)
我可能错了,但我相信问题是你在bindParam调用中使用“:memberID”。参数名称为“memberID”。
$query = "SELECT * FROM viewed WHERE memberID = :memberID ORDER BY id DESC LIMIT $per_page OFFSET $offset";
$stmt = $db->prepare($query);
$stmt->bindParam('memberID', $uid, PDO::PARAM_INT);