Check if exist or not = mysqli_num_rows() empty and counting

Posted under » PHP » MySQL updated on on 5 Oct 2022

Sometimes you need to know if your SQL query is empty. For example, is there someone living in Alaska in your table. If there nobody then you won't show the map of Alaska.

$result = mysql_query("select city from USA WHERE city = '$rid'" );
$rowcount = mysql_num_rows($result);
if ($rowcount > 0) { exit("Sorry. there is nothing to show.");  }
or if using mysqli
$result = mysqli_query("select city from USA WHERE city = '$rid'" );
$rowcount = mysqli_num_rows($result);
if ($rowcount > 0) { exit("Sorry. there is nothing to show.");  }

Here is how I use it

for ($i=1; $i<=10; $i++)
{
$result = mysqli_query($dbhandle,"SELECT * FROM vote where student = 7 and choice = $i");
$rowcount = mysqli_num_rows($result);
if ($rowcount == 0) { echo "

Sorry. there is nothing to show."; } while ($baongs = mysqli_fetch_object($result)) { echo "<p>".$baongs->choice."-".$rowcount; } }

Read about this practical use of pagination.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS IOT