Hi,
I have a function as below which works well when the query $queryItems finds a barcode but when the barcode does not exist in the barcode table the PHP script throws a
Warning: Invalid argument supplied for foreach() on line *** where the line no. corresponds to
foreach ($fromBarcode as $value){
$fromBarcode=getBarcode($barcode, $pdo);
foreach ($fromBarcode as $value){
echo $value.'<br>';
}
function getBarcode($barcode, $pdo){
$queryItems="
SELECT
commodities.commodityName as Food,
genre.genreName as Genre,
manufacturer.manufacturerName as Manufacturer,
packaging.packagingName as Packaging
FROM barcodes
LEFT JOIN commodities
ON barcodes.food=commodities.commodity_ID
LEFT JOIN genre
ON barcodes.genre=genre.genre_ID
LEFT JOIN manufacturer
ON barcodes.manufacturer=manufacturer.manufacturer_ID
Left JOIN packaging
ON barcodes.packaging=packaging.packaging_ID
WHERE barcodes.barcode='$barcode'";
//Prepare and execute the query
$stmtItems=$pdo->prepare($queryItems);
$stmtItems->execute();
//Cycle through the query result
foreach ($stmtItems as $value){
$food=$value['Food'];
$packaging=$value['Packaging'];
$manufacturer=$value['Manufacturer'];
$genre=$value['Genre'];
return array($food, $packaging, $manufacturer, $genre);
} //close foreach ($stmtItems as $value){
} //close function
What is the best way to deal with this? I have tried checking the result and Googling but I canât figure it out.
Thank you.