[英]Loop through a MySQL database until there is a result, then break
如果您查看else
部分,那么会有一个包含问题的代码注释。 如何重复它,也许使用另一种循环? 不幸的是,我不知道如何。
for ($id = 0; $id <= 16; $id++) {
$count1++;
$count2++;
$sql = "SELECT a FROM jedynki where id = $count1 && f = '0' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$a = $row['a'];
$a=substr($a,0,55);
// do something...
break;
}
} else {
/* If no results:
1. Go back to the top of sql query
2. Increment $count1
3. Try again
Repeat these steps (very important), until there is a result. */
}
$sql = " SELECT a FROM jedynki where id = $count2 && f = '1' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$b = $row['a'];
$b=substr($b,0,55);
?>
// do something...
<?php
}
}
}
我还无法测试,但据我所知它应该可以工作。 它增加count1
只要结果为空(环路尚未开始),或者是提取的行数为零,查询与新增加值数据库count1
。
for ($id = 0; $id <= 16; $id++) {
$result = NULL;
do {
$count1++;
$sql = "SELECT a FROM jedynki where id = $count1 && f = '0' ";
$result = $conn->query($sql);
} while(($result == NULL) || ($result->num_rows == 0));
while($row = $result->fetch_assoc()) {
// do something...
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.