我从mysql表(作业)中获取行。在该获取内部,我也从另一个表(帐户)获取[接收帐户api密钥全部取决于ID_ASSOC对作业的影响]:下面是代码
$sql = "SELECT * FROM jobs";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
echo $row['action'];
echo "<br/>";
$job_poster_id = $row['id_assoc'];
$sql = "SELECT * FROM accounts WHERE id_assoc='$job_poster_id'";
$query = mysqli_query($db_conx, $sql);
while($rows = mysqli_fetch_assoc($query)){
$username = $rows['twitter_username'];
$consumer_key = $rows['consumer_key'];
$consumer_secret = $rows['consumer_secret'];
$access_token = $rows['access_token'];
$access_token_secret = $rows['access_token_secret'];
}
echo $job_poster_id ;
echo "<br/>";
echo $twitter_username;
echo "<br/>";
echo "----------------------------------";
echo "<br/>";
}
输出:
specific-message
4
admin
----------------------------------
当我这样做时,我只得到一行输出......我似乎无法找出原因。我希望上面的内容重复次数与行数相同,并且它只执行一行(在代码中使用帐户获取)。但是当我没有内部提取(帐户提取)时,它会根据需要返回多行。为什么是这样? (以下是没有提取帐户的示例代码):
$sql = "SELECT * FROM jobs";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
echo $row['action'];
echo "<br/>";
$job_poster_id = $row['id_assoc'];
echo $job_poster_id ;
echo "<br/>";
echo "----------------------------------";
echo "<br/>";
}
输出:
specific-message
4
----------------------------------
specific-message
1
----------------------------------
specific-message
2
----------------------------------
答案 0 :(得分:1)
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
echo $row['action'];
echo "<br/>";
$job_poster_id = $row['id_assoc'];
$sql = "SELECT * FROM accounts WHERE id_assoc='$job_poster_id'";
$query = mysqli_query($db_conx, $sql);
问题在于您使用$query
作为内部和外部查询。
当内部查询运行并且它遍历循环时,它会迭代到结果集的末尾;当外部while循环运行时,mysqli_fetch_assoc($query)
返回false,因为您已经在结果集的末尾 - 而不是您期望的结果集。
您可以通过重命名其中一个$query
变量来解决此问题。