SQL Select produces slightly different results in phpAdmin vs. phpHTML
I consistently skip the result of the first row, which is more noticeable when there is only one row result.
I have a problem with my PDO commands. Any suggestions for a fix please? If I remove $ 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();
?>
source to share
You are duplicating $row = $q->fetch(PDO::FETCH_ASSOC);
.
When you ask $q to $row
, it $q->fetch
is cleared (no data), so IF sentence
you have no rows to retrieve in $ q. You have to remove $row = $q->fetch(PDO::FETCH_ASSOC);
and just use it in IF.
Also try doing fetchAll () before $q
.
$result = $query -> fetchAll();
foreach( $result as $row ) {
/*CODE*/
}
source to share