I have a while loop that iterates through the rows returned from a DB query.
Query
$sql = "SELECT t.barcode AS barcode, t.code, t.brand, t.name, t.cost, t.price, t.vat, SUM(n.stock) AS stock
FROM t
INNER JOIN c.am ON t.code=am.code
INNER JOIN n ON t.code=n.code WHERE (n.name='X' OR n.name='Y')
AND t.code IN ($in)
GROUP BY t.code, t.name, t.cost, t.salesprice, t.vat";
$result = $mysqli->query($sql);
echo "<table id='maintable'><tr><th>Barcode</th><th>Name</th><th>Brand</th></tr>";
while($row = $result->fetch_array()) {
if(empty($row['barcode'])){
$barcode = "none";
}
$barcode = $row['barcode'];
$name = $row['name'];
$brand = $row['brand'];
echo "<td>" . $barcode . "</td>";
echo "<td>" . $name . "</td>";
echo "<td>" . $brand . "</td>";
}
echo "</table>";
The problem is if barcode is empty (not in the DB table), no rows get displayed. However if there is a barcode, the row DOES display. In my example I have tried to check if $row['barcode'] is empty to assign a string so that the row will still display but its unsuccessful.
In the Table itself in the database, the Barcode has a Null field set to YES and Default Field set to NULL so I have also tried:
if(is_null($row['barcode'])) { ..... };
But unsuccessful.
Somewhere I read that empty can equate to: '', 0 and NULL so I'm thinking it's failing because checking if "empty" is the wrong approach? Any help appreciated.