简体   繁体   中英

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. Any suggestions for how to correct please? If I remove the $pod->prepare nothing works. 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. This has nothing to do with the value of the line_item_id database column.

You are getting a PHP error. The variable $line_item_id is undefined.

You are duplicating $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. You have to remove $row = $q->fetch(PDO::FETCH_ASSOC); and just use it in the IF.
Also try to do a fetchAll() to $q .

$result = $query -> fetchAll();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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