dongqiuxu2270 2018-04-08 04:49
浏览 125

mysqli_fetch_assoc仅在迭代时才返回数组的第二个元素

This is my code:

$query2 = "select * from teaches where teacherid_fk LIKE '$teacherid_fk'";
$userinfo2 = mysqli_query($conn, $query2);
while ($line = mysqli_fetch_assoc($userinfo2)) {echo json_encode($line);}

Somehow, the result I'm getting is only one array when there should be two arrays displayed. I need both arrays to be displayed, but my code only displays the second array. This is the db: dbTableofTeaches

Am I doing something wrong here? Just recently learned PHP and any help is deeply appreciated. :)

EDIT 2: If I do the code like this:

$query2 = "select * from teaches where teacherid_fk LIKE '$teacherid_fk'";
$userinfo2 = mysqli_query($conn, $query2);
$row = mysqli_fetch_array($userinfo2);
echo json_encode($row);

The result is this: {"teaches_id":"2","teacherid_fk":"1","subjectid_fk":"65","quarterid_fk":"1","gradeid_fk":"5","sectionid_fk":"13","schoolyearid_fk":"2"} [which is actually the first array I was looking for!]

EDIT 3: Okay I'm getting more confused. I've tried to edit the db so it now looks like this: dbTableOfTeaches2. With the same (first) code above, I refreshed the page, and the second and third arrays are now displayed, but still no sign of the first array! The only way I was able to display the first array was use the second code above.

  • 写回答

1条回答 默认 最新

  • duanlan3598 2018-04-08 06:46
    关注

    try this.

    $query2 = "select * from teaches where teacherid_fk = '$teacherid_fk'";
    $userinfo2 = mysqli_query( $conn, $query2 );
    
    $respons = array();
    
    while ( $line = mysqli_fetch_assoc( $userinfo2 ) ) {
    
        $respons[] = $line;
    
    }
    
    echo json_encode($respons);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败