我正在尝试获取一列等于该列中的值的结果,我的代码运行时从查询中删除了where子句,但没有引发任何错误,但是foreach不运行。
$themes = Singlequery ('SELECT * FROM items WHERE type = :theme ORDER BY id = :id DESC LIMIT 5',
array('theme' => ['theme'], 'id' => ['id']), $conn);
<?php foreach ($themes as $theme) : ?>
<li><a href="#"><?= $theme['name']; ?></a></li>
<?php endforeach; ?>
这就是我的功能,这就是为什么我要绑定的原因。
function Singlequery($query, $bindings, $conn)
{
$stmt = $conn->prepare($query);
$stmt->execute($bindings);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
您的参数数组错误,应该是:
array(':theme' => $theme, ':id' => $id)
注意:
其中。同样,您的值实际上是数组。当PDO开始绑定时,它将期待字符串,并找到一个数组,因此最有可能您的查询(如果参数首先生效)将产生以下内容:
SELECT ... WHERE type = 'Array' ORDER BY id = 'id'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句