返回MySQL 2行,只显示1个内容

时间:2014-02-06 16:39:45

标签: php mysql prepared-statement

所以我遇到MySQL返回2行的问题,但只有1行显示在变量中。 不确定为什么它不返回

        $sql->bind_result($c_id, $c_location, $c_type) or die($mysqli_load->error);
        while($row = $sql->fetch()){
        $mysqli_load2 = new mysqli(HOST, USER, PASS, DB);
        $query = "SELECT `badge` FROM `responders` WHERE `cid` = ?";
        $sql2 = $mysqli_load2->prepare($query) or die($mysqli_load2->error);
        $sql2->bind_param('i', $c_id);
        $sql2->execute() or die($mysqli_load2->error);
        $sql2->store_result();
        $rows = $sql2->num_rows;
        $sql2->bind_result($units);
        $sql2->fetch();
        $sql2->close();
        $mysqli_load2->close();
          echo '
          <tr>
            <td align="justify"><a href="viewcall.php?cid=' . $c_id .'"><u><abbr title="View Call">'.$c_id.'</abbr></u></td>
            <td align="justify">' . $c_location .'</td>
            <td align="justify">' . $c_type .'</td>
            <td align="justify">Rows: ' . $rows . ' ' . $units . '</td></tr>'; 
          } 

2 个答案:

答案 0 :(得分:2)

你需要使用fetch()方法循环:

while ($sql2->fetch()) {
  echo $units;
}
$sql2->close();

答案 1 :(得分:0)

因此,对于任何有类似问题的人来说,这是新的更新代码:

        $sql->bind_result($c_id, $c_location, $c_type) or die($mysqli_load->error);
        while($row = $sql->fetch()){
            $mysqli_load2 = new mysqli(HOST, USER, PASS, DB);
            $query = "SELECT `badge` FROM `responders` WHERE `cid` = ?";
            $sql2 = $mysqli_load2->prepare($query) or die($mysqli_load2->error);
            $sql2->bind_param('i', $c_id);
            $sql2->execute() or die($mysqli_load2->error);
            $sql2->bind_result($units);
                    echo '
            <tr>
            <td align="justify"><a href="viewcall.php?cid=' . $c_id .'"><u><abbr title="View Call">'.$c_id.'</abbr></u></td>
            <td align="justify">' . $c_location .'</td>
            <td align="justify">' . $c_type .'</td>
            <td align="justify">'; 
             while($sql2->fetch())
             {
             echo
             '#'.$units.' '; } echo '</td></tr>';
             } 
             $sql2->close();
             $mysqli_load2->close();
          $sql->close();
          $mysqli_load->close();