而查询没有得到第一项

时间:2018-12-10 08:49:38

标签: php mysql

我的查询确实获得了项目,但不是全部。冷杉并没有受到谴责。我该怎么办?

<?php
$id = (int) $_SESSION['id'];

$query = $conn->query ("SELECT * FROM transaction WHERE customerid = '$id' ") or die (mysqli_error());
$fetch = $query->fetch_array ();
$customer_id=$fetch['customerid'];
if($id!=$customer_id){
    echo "<table>
        <tr>
            <td>No Order Yet!</td>
        </tr>
    </table>";
}else if($id==$customer_id){
    while($row=$query->fetch_array ()){
        echo"
        \\code
        ";  
    }
}
?>

1 个答案:

答案 0 :(得分:0)

您两次致电fetch_array (),这会使您错过第一条记录。如果我正确理解了您的逻辑,请尝试输出语句中的所有行,然后进行其他检查以获取空结果集。

<?php
    # Statement
    $id = (int) $_SESSION['id'];
    $query = $conn->query("SELECT * FROM transaction WHERE customerid = '$id' ") or die (mysqli_error());

    # Rows output
    while ($row = $query->fetch_array()) {
        echo "";
    }

    # Empty result. Check record count after all the rows in the result have been retrieved.
    if ($query->num_rows = 0) {
        echo 
            "<table>
            <tr><td>No Order Yet!</td></tr>
            </table>";
    )       
?>