测试 if 语句的不确定量的布尔值
我有一个 for 语句,它运行一些 mysql 查询,但它们不会总是相同数量的查询,如下所示:
for($i=0;$i<count($phones);$i++){
$results[$i] = mysql_query("[ACTUAL MYSQL QUERY]");
}
如您所见,我将结果放入一个数组中,并且我想使用if 语句。我该怎么做,我在想 eval() ,但这似乎不可靠。
I have a for statement that runs a few mysql querys but they wont always be the same number of querys like this:
for($i=0;$i<count($phones);$i++){
$results[$i] = mysql_query("[ACTUAL MYSQL QUERY]");
}
As you can see I put the result into an array, and I want to make sure all of the queries were successful using an if statement. How would I do that, I was thinking eval()
but that doesn't seem reliable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
array_filter
一次性检查整个 $result 列表,因为对于失败的查询,mysql_query
将返回false
。(我相信如果你打开太多并发查询句柄,你可能会遇到问题;它们会消耗一些内存。所以也许你应该在循环中立即执行
mysql_fetch_assoc
,或者只保留一个布尔值,然后释放结果句柄。)You can use
array_filter
to check the whole $result list at once, becausemysql_query
will returnfalse
for failed queries.(I believe you might however run into problems if you keep too many concurrent query handles open; they consume some memory. So maybe you should rather do an immediate
mysql_fetch_assoc
in your loop, or just keep a boolean, then free the result handles.)在
for
循环之前声明一个值为 true 的布尔变量。然后,当您循环时,如果任何值为 false,则将变量设置为 false。如果循环后布尔变量仍然为 true,则查询全部成功。Before the
for
loop declare a boolean variable that has the value of true. Then as you loop through, if any of the values are false set the variable to false. The queries were all successful if the boolean variable is still true after the loop.