简体   繁体   中英

Checking if Joined Table is Empty [PHP & MYSQL]

How do I check if my statement is empty? The below code just show nothing if the Join contains nothing. How do i get it to display the first part of my IF statement ( Your To-Do List is Empty! )?

<?php 

  $statement = $db->query('SELECT * FROM todo_item as ti INNER JOIN todo_category as tc ON ti.todo_id = tc.todo_id');

  if (empty($db))
  {
    echo "<p style='background-color:green;'><strong>Your To-Do List is Empty!</strong></p>";
  }
  else
    foreach($statement as $row): 
?>

you can do a mysql_num_rows and see how many rows are being returned. if that is 0, then you dont have anything in the db that matches the query

Are you using PDO? PDOStatement::rowCount

try @mysql_num_rows($statement);

Another way to count rows is by using $row = mysql_fetch_row($statement); $records = $row[0];

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