douzi5214 2013-08-26 19:47
浏览 21
已采纳

mysqli结果的数量取决于内部的提取

I am fetching rows from a mysql table (jobs). Inside of that fetch, I am also fetching from another table (accounts) [to receive account api keys all depending on what ID_ASSOC is attacted to the job]: below is the code

$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/>";
}

OUTPUT:

specific-message 
4 
admin
----------------------------------

When I do this, I only get one row output..and I can't seem to find out why. I want the above out put to repeat as many times as it has rows, and it's only doing one row (with the account fetch in the code). However when I do it without the internal fetch (accounts fetch), it returns multiple rows just as desired. Why is this? (below is sample code WITHOUT the accounts fetch):

$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/>";
} 

OUTPUT:

specific-message
4
----------------------------------
specific-message
1
----------------------------------
specific-message
2
----------------------------------
  • 写回答

1条回答 默认 最新

  • dongxing2015 2013-08-26 19:59
    关注
    $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);
    

    The problem is that you're using $query for the inner and the outer query.

    When the inner query runs, and it steps through the loop, it's iterating to the end of the result set; when the outer while loop runs, mysqli_fetch_assoc($query) is returning false, because you're already at the end of the result set - just not the result set you were expecting.

    You can fix this by renaming one of the $query variables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?