Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 而查询不获取第一项_Php_Mysql - Fatal编程技术网

Php 而查询不获取第一项

Php 而查询不获取第一项,php,mysql,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']; i

我的查询确实获得了项目,但不是全部。第一个不会被执行。我该怎么办

<?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
        ";  
    }
}
?>

调用
fetch\u 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>";
    )       
?>

$query->fetch\u array()
one,
$query->fetch\u array()
two。因为您已经在第5行获取了它。还要学习如何编写准备好的语句,因为您的代码容易受到SQL注入的攻击。您是否使用pdo或mysqli