这个PDO :: FETCH_ASSOC`查询会跳过返回的第一个结果 [英] This PDO::FETCH_ASSOC` query skips the 1rst result that's returned
问题描述
我正在过渡到PDO准备的语句,并且在使用WHILE
语句的基本SELECT
查询的语法上遇到了麻烦.
下面的foreach
语句回显正确的结果,但是PDO::FETCH_ASSOC
查询正在跳过返回的第一个结果(因此它总是回显比其应有的结果少的结果).
PDO :: FETCH_ASSOC
$stmt = $conn->prepare("SELECT * FROM products");
$stmt->execute();
$row = $stmt->fetch();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['product_name'].' '.$row['price'].' '.$row['color'], "<br />";
}
foreach
foreach($conn->query('SELECT * FROM products') as $row) {
echo $row['product_name'].' '.$row['price'].' '.$row['color'], "<br />";
}
您已经获取了while循环$row = $stmt->fetch();
之前的第一行.如果删除此行,它将按预期工作.
由于while循环将在每次迭代时覆盖$row
,因此看起来就像从第二行开始,但是发生的情况是在第一次while循环迭代中$row
的值被覆盖了.
要使循环以您编写的方式工作,您需要使用构造:
$row = $stmt->fetch();
do {
echo $row['product_name'].' '.$row['price'].' '.$row['color'], "<br />";
} while($row = $stmt->fetch(PDO::FETCH_ASSOC));
在$row
条件覆盖之前,将首先打印$row
的值.
在这种情况下,我不想在没有任何结果的情况下回显任何东西
是这种情况,然后检查查询是否首先返回了任何结果.在这里,我在检查中是明确的,因为如果删除了外部的if
,则您的while
循环仍会按照您的意图进行-也就是说,如果没有任何结果,它将不会回显任何内容.>
但是,最好在代码中明确意图:
if ($stmt->columnCount()) {
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['product_name'].' '.$row['price'].' '.$row['color'], "<br />";
}
}
I'm transitioning over to PDO prepared statements, and I'm having trouble with the syntax for a basic SELECT
query with a WHILE
statement.
The foreach
statement below echos the correct results, but the PDO::FETCH_ASSOC
query is skipping the 1rst result that's returned (so it always echo's one result less than it should).
PDO::FETCH_ASSOC
$stmt = $conn->prepare("SELECT * FROM products");
$stmt->execute();
$row = $stmt->fetch();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['product_name'].' '.$row['price'].' '.$row['color'], "<br />";
}
foreach
foreach($conn->query('SELECT * FROM products') as $row) {
echo $row['product_name'].' '.$row['price'].' '.$row['color'], "<br />";
}
You already fetched the first row before the while loop $row = $stmt->fetch();
. If you remove this line, it will work as expected.
Since the while loop will overwrite $row
on each iteration, it looks like you start with the second row, but what happens is the value of $row
at the first while loop iteration is overwritten.
To have the loop work the way you have written, you would need to use a do-while construct:
$row = $stmt->fetch();
do {
echo $row['product_name'].' '.$row['price'].' '.$row['color'], "<br />";
} while($row = $stmt->fetch(PDO::FETCH_ASSOC));
Here the value of $row
will be printed first, before it is overwritten by the while
condition.
In this particular case I don't want to echo anything when there aren't any results
If that's the case, then check to see if your query returned any results first. Here I'm being explicit in the check, because if you removed the outer if
, your while
loop would still follow your intentions - that is, it won't echo anything if there aren't any results.
However, it is always good to have clear intent in your code:
if ($stmt->columnCount()) {
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['product_name'].' '.$row['price'].' '.$row['color'], "<br />";
}
}
这篇关于这个PDO :: FETCH_ASSOC`查询会跳过返回的第一个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!