Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Select producing slightly different results in phpAdmin vs. phpHTML

Tags:

php

select

mysql

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();
?>
like image 543
Bricked Avatar asked Mar 27 '26 23:03

Bricked


1 Answers

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*/
}
like image 83
Roy Bogado Avatar answered Mar 31 '26 04:03

Roy Bogado