简体   繁体   English

SQL选择在phpAdmin与phpHTML中产生略有不同的结果

[英]SQL Select producing slightly different results in phpAdmin vs. phpHTML

I consistently miss the first row result, which is more noticeable when there is only one row result. 我一直错过第一行结果,当只有一行结果时更明显。

I have a problem with my PDO commands. 我的PDO命令有问题。 Any suggestions for how to correct please? 有关如何纠正的任何建议吗? If I remove the $pod->prepare nothing works. 如果我删除$ pod->准备无效。 Not sure what to do? 不知道该怎么办?

<?php
$sql = "SELECT  * FROM Order_Items 
        JOIN    Parts ON Parts.id = Order_Items.part_id
        WHERE   Order_Items.orders_id = $id
        AND     qty <> 0
        ORDER BY Parts.id";

        $q = $pdo->prepare($sql);
        $q->execute(array());
        $row = $q->fetch(PDO::FETCH_ASSOC); // Roy says this is not needed

        while ($row = $q->fetch(PDO::FETCH_ASSOC)) 
        {
            echo    '<tr>';
            echo    '<td>' . $row['part_num'] . '</td>';
            echo    '<td>' . $row['part_desc'] . '</td>';
            echo    '<td>' . $row['qty'] . '</td>';
        }

    Database::disconnect();
?>

You are not getting an SQL error. 您没有收到SQL错误。 This has nothing to do with the value of the line_item_id database column. 这与line_item_id数据库列的值无关。

You are getting a PHP error. 您收到PHP错误。 The variable $line_item_id is undefined. 变量$line_item_id未定义。

You are duplicating $row = $q->fetch(PDO::FETCH_ASSOC); 您正在复制$row = $q->fetch(PDO::FETCH_ASSOC); .
When you asing $q to $row , $q->fetch is cleared (with no data) so in the IF sentence you have no rows to fetch in $q. 当你将$q to $row$q->fetch被清除(没有数据)所以在IF sentence你没有要在$ q中获取的行。 You have to remove $row = $q->fetch(PDO::FETCH_ASSOC); 你必须删除$row = $q->fetch(PDO::FETCH_ASSOC); and just use it in the IF. 并在IF中使用它。
Also try to do a fetchAll() to $q . 还尝试对$q执行fetchAll()

$result = $query -> fetchAll();

foreach( $result as $row ) {
    /*CODE*/
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM