Bricked
Bricked

Reputation: 115

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();
?>

Upvotes: 3

Views: 63

Answers (2)

Roy Bogado
Roy Bogado

Reputation: 4452

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*/
}

Upvotes: 1

Quentin
Quentin

Reputation: 944204

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.

Upvotes: 1

Related Questions