0

I'm not managing to display a message nothing found when the table is empty this query, someone could say where I am going wrong?

<?php
      $sqlRead = "SELECT * FROM dados ORDER BY name ASC";

        try{
            $read = $db->prepare($sqlRead);
            $read->execute();
        } catch (PDOException $e) {
                echo $e->getMessage();
        }
        while ($rs = $read->fetch(PDO::FETCH_OBJ)){

?>

 <h1><?php echo $rs->name; ?></h1>
  • Could you elaborate on your question a bit? Are you not getting an "empty" message, or is nothing being shown even though records exist? If the first, your code doesn't show any sign of echoing a message on an empty table. I'd advise reading this: [How do I ask a good question](http://stackoverflow.com/help/how-to-ask) – CmdrSharp Nov 05 '16 at 05:01
  • Do you try the `num_rows` before? – Felix Fong Nov 05 '16 at 05:09
  • Sorry, it's my first time on the site. I do not know how to put an "if" to display a "nothing found" message when $ sqlRead does not return anything, that is, when the table in mysql is empty. If anyone can help by implementing the code I posted. I tried num_rows but it did not work. – joaovictormacedo Nov 09 '16 at 02:17

1 Answers1

0

I found the solution

<?php
  $sqlRead = "SELECT * FROM dados ORDER BY name ASC";

    try{
        $read = $db->prepare($sqlRead);
        $read->execute();
        $result = $read->rowCount();
    } catch (PDOException $e) {
            echo $e->getMessage();
    }

    if($result == 0){
       echo "NOTHING FOUND!";
    } else {
       while ($rs = $read->fetch(PDO::FETCH_OBJ)){
?>

 <?php echo $rs->name; ?>

<?php } } ?>

With the code above, can display the message if nothing is found in the database