我一直试图循环这个查询,但我得到的只是第一个值。
当我在工作台中执行相同的命令时,我获得了所有值。 我在这做错了什么?任何答案都非常感谢!
global $db;
$stmt12 = $db->query('SELECT `Value` FROM overriddenpropertyvalues WHERE ParentGUID LIKE "' . $itemguid . '";');
$propertyvaluerow = $stmt12->fetch();
while ($propertyvaluerow != null) {
答案 0 :(得分:0)
您只使用->fetch()
获取一个值。这就是为什么你只收到一个值。
来自here的示例:
$query = $db->prepare('SELECT `Value` FROM `overriddenpropertyvalues` WHERE ParentGUID LIKE :like');
$query->execute([':like' => $itemguid]);
$stmt->bind_result($value);
while ($query->fetch()) {
echo $value."<br/>"
}
答案 1 :(得分:0)
您的计划可能存在漏洞,具体取决于 $ stmt12-&gt; fetch()的漏洞。如果它获取数据数组,则while循环可能不会按预期运行。以下是您可能想要尝试的评论替代方案:
<?php
global $db;
$stmt12 = $db->query('SELECT `Value` FROM overriddenpropertyvalues WHERE ParentGUID LIKE "' . $itemguid . '";');
// ASSUMES YOU ARE USING PDO SO WE FETCH ALL THE DATA
$propertyvaluerow = $stmt12->fetchAll();
// THERE MIGHT BE A LOOPHOLE IN YOUR PROGRAM DEPENDING ON WHAT $stmt12->fetch() IS AND DOES
// ASSUMING IT FETCHES AN ARRAY OF NESTED OBJECTS OR SCALAR VALUES, THE WHILE LOOP WOULD NOT BEHAVE AS EXPECTED.
// THAT MEANS IF IT IS AN ARRAY YOU COULD USE A DIFFERENT CONSTRUCT LIKE THE ONE BELOW YOUR WHILE CONSTRUCT:
/* while ($propertyvaluerow != null) { */
// CREATE A $count VARIABLE TO HOLD THE INCREMENTAL COUNT THROUGH THE ITERATION:
$count = count($propertyvaluerow);
while($count > 0){
// DO YOUR WORK HERE
//DECREMENT THE VALUE OF COUNT OTHERWISE YOU MAY HAVE AN INFINITE LOOP TO DEAL WITH.
$count--;
}
?>
还有另一种选择:
<?php
// OR EVEN A MUCH MORE EASIER WAY IS TO USE THE FOREACH LOOP, WHICH ACHIEVES THE SAME THING AS THE WHILE LOOP:
foreach($propertyvaluerow as $iKey=>$objData){
// SIMPLY USE THE $objData IN WITHING THE LOOP
// THE $objData IS THE VALUE OF THE CURRENT OBJECT IN THE $propertyvaluerow IN THE ITERATION
}
答案 2 :(得分:0)
最佳方式:
global $db;
$stmt = $db->prepare('SELECT `Value` FROM OverriddenPropertyValues WHERE ParentGUID=?');
$stmt->execute([$itemguid]);
$rows = $stmt->fetchAll();
foreach($rows as $row) {
// $row->Value or $row['Value']
}