简体   繁体   中英

PHP MYSQL Get data from database and echo message if no data retrieved

Im trying to make a code where it displays all names in database where "clanwars" is set to 1 (int) and if all of them is 0 echo an message like: "Noone has signed up yet!"

This is some of the code i have:

$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
while($row = mysqli_fetch_array($result))
  {
    if ($row['clanwars'] != '0') {
          echo $row['mantra']."<br>";
    } else {
        echo 'Noone has signed up yet!';
    }
    }

mysqli_close($con);

First, in your example row['clanwars'] will never equal to 0 because you already specified WHERE clanwars = 1 in your query, so MySQL will return only those that have clanwars=1. If I understand well, you need to do something like:

<?
  $result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
  if (mysqli_num_rows($result)==0) echo 'Noone has signed up yet';
  else {
    while ($row = mysqli_fetch_array($result)) {
      //do what you need
    }
  }
?>

So basically, you retrieve everyone who has CLANWARS set to 1 in the database. If there are records, process them, if there are no records, it means that nobody has signed up.

Is this what you need?

Try this:

$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
if (mysqli_num_rows($result) == 0)
{
    echo 'Noone has signed up yet!';
}
else
{    
   while ($row = mysqli_fetch_array($result))
   {
       echo $row['mantra']."<br>";
   }
}

mysqli_close($con);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM