PHP how to handle when there is no foreach loop? -
i have foreach loop runs without flaw.
foreach ($row $row) { echo $row['id']; }
there times there no rows returned. want echo out 'there no rows'; when there no rows. problem if try follows:
foreach ($row $row) { if (!isset($row['id'])) { echo 'there no rows'; } else { echo $row['id']; } }
it never returns echo "there no rows". assume because when there no rows, foreach loop doesn't run. question becomes, how echo "there no rows" if , if there no rows while not interfering foreach when there rows.
i have tried code such as:
$row1 = $stmt->fetch(); $row = $stmt->fetchall(); if (isset($row1['id'])) { foreach ($row $row) {
still no luck
so desired outcome follows:
when loop runs: 1 2 3 4 when loop doesn't run: there no rows
you should check before loop so
if(count($row)>0){ //foreach ... }else{ echo 'no data'; }
Comments
Post a Comment