php - 如何在php的while循环中执行mysql查询
问题描述
我的 mysql 连接有问题,我想在 mysqli_fetch_assoc 的一段时间内执行查询,但是,我收到类似“命令不同步;您现在无法运行此命令”之类的错误。
<?php
$sql = "call sp_getUpline('$user_id')";
$result = mysqli_query($connection,$sql);
$numrow = mysqli_num_rows($result);
if($numrow > 1)
{
while($resultArray = mysqli_fetch_assoc($result))
{
$parent_id = $resultArray['parent_id'];
if($parent_id != null)
{
$Level = $resultArray['Level'] + 1;
$comp_sql = "SELECT * FROM user_comp_plan where id='$parent_id'";
$comp_result = mysqli_query($connection, $comp_sql) or die(mysqli_error($connection)); //getting error here
}
}
}
?>
更新
我将结果存储在数组中,但仍然遇到同样的问题。
<?php
$sql = "call sp_getUpline('$user_id')";
$result = mysqli_query($connection,$sql);
echo $numrow = mysqli_num_rows($result);
if($numrow > 1)
{
$data = array();
while($resultArray = mysqli_fetch_assoc($result))
{
$data[] = $resultArray;
}
for($i = 0; $i < count($data); $i++){
$parent_id = $data[$i]['parent_id'];
if($parent_id != null)
{
$Level = $data[$i]['Level'] + 1;
$comp_sql = "SELECT * FROM user_comp_plan where id='$parent_id'";
$comp_result = mysqli_query($connection, $comp_sql) or die(mysqli_error($connection)); //getting error here
}
}
}
?>
解决方案
您不能同时进行两个查询,因为 mysqli 默认使用无缓冲查询。您可以将第一个获取到数组中并循环遍历,或者告诉 mysqli 使用 mysqli::store_result 缓冲查询
推荐阅读
- android - Flutter-如何制作多个 notifyListeners()?
- sql-server - 更新和删除不起作用 vb.net
- symfony - symfony easyadmin 新文章被提交和存储,但没有 author_id
- security - 如何在 Spring Boot 应用程序中测试 Keycloak 身份验证?
- propel - Propel3 发电机位置,propel-gen
- python - Python 不写入 Arduino 串行
- c# - 如何以编程方式创建 ASP.NET MVC 项目?
- python - 更新已转换为 Windows 应用程序的 Python 应用程序的最佳方法是什么
- sas - 检查表是否存在,如果不存在则什么都不做
- ios - 对于 MVVM 设计模式,表格视图单元格高度属于哪里?