2

I'm querying a database but when the result is empty i want to output a table row displaying "nothing to display" but the if seems to always return true.

Here's my code...

$priorityincidentsQ         = mysql_query("SELECT * FROM applications WHERE pi >= ('2') ");
while($priorityincidentsR   = mysql_fetch_object($priorityincidentsQ)) 
  {
    if (empty($priorityincidentsR)) {
      echo "<tr><td class=\"closedcallscell centered\"><b>Nothing to display</b></td></tr>";    
    } else {        
      echo "<tr><td class=\"closedcallscell\"><b>$priorityincidentsR->application_friendly_name</b></td>";
      echo "<td  class=\"closedcallscell table_row_small\"><center>$priorityincidentsR->pi</center></td></tr>"; 
    }
  }
xpy
  • 5,481
  • 3
  • 29
  • 48
Matt Bryce
  • 123
  • 1
  • 8
  • 2
    [**`mysql_*` is deprecated and is no longer included in PHP](http://stackoverflow.com/questions/13944956/the-mysql-extension-is-deprecated-and-will-be-removed-in-the-future-use-mysqli)**. – Ben Feb 26 '16 at 10:32
  • 1
    did you try to var_dump the priorityincidentsR value? – rvandoni Feb 26 '16 at 10:36
  • What exactly does your table show after your code executes? In theory, your if statement would never even run if your result didn't return any rows because of the condition on the while loop. If there were no rows then `mysql_fetch_object()` would not be evaluated as true. There must be an error in your query if you are even seeing the results of the if statement at all. – shamsup Feb 27 '16 at 06:05

2 Answers2

4

Use mysqli_num_rows() to check is there any result:

    $conn = mysqli_connect($host, $user, $password, $database);
    $priorityincidentsQ = mysqli_query($conn, "SELECT * FROM applications WHERE pi >= ('2') ");
    if (mysqli_num_rows($priorityincidentsQ) > 0){
        while ($priorityincidentsR = mysqli_fetch_object($priorityincidentsQ)) {
            echo "<tr><td class=\"closedcallscell\"><b>$priorityincidentsR->application_friendly_name</b></td>";
            echo "<td  class=\"closedcallscell table_row_small\"><center>$priorityincidentsR->pi</center></td></tr>";
        }
    }else{    
        echo "<tr><td class=\"closedcallscell centered\"><b>Nothing to display</b></td></tr>";
    }

And yes, better use mysqli_* functions instead of mysql_*.

mitkosoft
  • 5,262
  • 1
  • 13
  • 31
  • it would seem the if is always evaluating to true... I suspect it's treating 0 as false somehow. – Matt Bryce Feb 26 '16 at 11:59
  • which one? `mysqli_num_rows()` returns exact number of rows, so if is always `true` means that you always has data returned, so better check your SQL query. – mitkosoft Feb 26 '16 at 12:02
0

Still never figured out why this wouldn't work out for me this way, I tried the query directly in SQL workbench and everything looks how it should, I ended up solving the problem like this.

<!-- priority incidents-->
<?php

$priorityincidentsQ         = mysql_query("SELECT * FROM applications WHERE pi >= ('1') ");
while($priorityincidentsR   = mysql_fetch_object($priorityincidentsQ)) 
    {
    echo "<tr><td class=\"closedcallscell\"><b><a href=\"".DIR."?p=$priorityincidentsR->pageID\">$priorityincidentsR->application_friendly_name</a></b></td>";
    echo "<td  class=\"closedcallscell table_row_small\"><center>$priorityincidentsR->pi</center></td></tr>";
    }

?>

<!-- if no incidents-->
<?php

$incidentNumberofRowsQ      = mysql_query("SELECT COUNT(*)numberofrows FROM applications WHERE pi >= ('1') ");
while($incidentNumberofRowsR    = mysql_fetch_object($incidentNumberofRowsQ)) 
    {

        if ($incidentNumberofRowsR->numberofrows == '0')
        {
                echo "<tr><td class=\"closedcallscell centered\"><b>Currently no priority incidents</b></td>";
        }
    }

?>

may seem a rather silly way of going about it but atleast it works. Thanks all for the help. :)

Matt Bryce
  • 123
  • 1
  • 8