如果只找到记录,则显示表格

What I mean is when there is a record found from the select result, the table is show up. But when there is no record, I don't want to show the table, but maybe with else statement.

I use this code:

       <?php
       $referenceNumber = $_POST['referenceNumber'];
       $result = $mysqli->query ("SELECT anything what I want

       if ($result > 0) {
         echo '<table id="orderHistoryTable" width=100% border=1 style="text-align:center">';                                
         echo '<tr>';
         echo '<td>ITEMS</td>';
         echo '<td>QUANTITY</td>';
         echo '<td>Infistall Location</td>';
         echo '<td>Infistall Address</td>';
         echo '</tr>';
          while($obj = $result->fetch_object()) {
             echo '<tr>';
             echo '<td>'.$obj->items.'</td>';
             echo '<td>'.$obj->esquantity.'</td>';
             echo '<td>'.$obj->infistall_Location.'</td>';
             echo '<td>'.$obj->infistall_address.'</td>';
             echo '</tr>';
           }
             echo '<tr><td colspan="3"></td>
                       <td><input type="submit" /></td>
                   </tr>';
        }

      ?>
     </table>

I have replace the way I echo it. Sometimes it loops the table. And with the code above it doesn't loop the table, but it shows the first <tr>...</tr> of the column title.

Thanks for easily understand my problem.

use

if($results->num_rows === 0)
{
   echo 'No results';
}
else
{
echo '<table id="orderHistoryTable" width=100% border=1 style="text-align:center">';                                
         echo '<tr>';
         echo '<td>ITEMS</td>';
         echo '<td>QUANTITY</td>';
         echo '<td>Infistall Location</td>';
         echo '<td>Infistall Address</td>';
         echo '</tr>';
          while($obj = $result->fetch_object()) {
             echo '<tr>';
             echo '<td>'.$obj->items.'</td>';
             echo '<td>'.$obj->esquantity.'</td>';
             echo '<td>'.$obj->infistall_Location.'</td>';
             echo '<td>'.$obj->infistall_address.'</td>';
             echo '</tr>';
           }
             echo '<tr><td colspan="3"></td>
                       <td><input type="submit" /></td>
                   </tr>';
}

Put your end tag within if statement (within curly brace }). It should be within the same if statement as .

use mysqli_num_rows check results

for example you can use

if(mysqli_num_rows($result) > 0){
     echo "<table>"
else
     echo "No results found";

We asked almost the same question.

check here: Checking and Returning values from database

and see if it helps !

You are right just us else statement like this

<?php
   $referenceNumber = $_POST['referenceNumber'];
   $result = $mysqli->query ("SELECT anything what I want");

   echo '<table id="orderHistoryTable" width=100% border=1 style="text-align:center">';                                
   echo '<tr>';
   echo '<td>ITEMS</td>';
   echo '<td>QUANTITY</td>';
   echo '<td>Infistall Location</td>';
   echo '<td>Infistall Address</td>';
   echo '</tr>';
   if ($result > 0) {

      while($obj = $result->fetch_object()) {
         echo '<tr>';
         echo '<td>'.$obj->items.'</td>';
         echo '<td>'.$obj->esquantity.'</td>';
         echo '<td>'.$obj->infistall_Location.'</td>';
         echo '<td>'.$obj->infistall_address.'</td>';
         echo '</tr>';
       }
         echo '<tr><td colspan="2"></td>
                   <td><input type="submit" /></td>
               </tr>';
    } else {
       echo '<tr><td colspan="4">NO Records Found</td></tr>';
    }

  ?>
 </table>

You can use:

$row_cnt = mysqli_num_rows($result);

and if condition be like:

if ($row_cnt > 0) 

mysqli_num_rows

You can use this code definitely work.

<?php
echo '<table id="orderHistoryTable" width=100% border=1 style="text-align:center">';         
$referenceNumber = $_POST['referenceNumber'];
$result = $mysqli->query("SELECT anything what I want");
if($result->num_rows() > 0){                       
        echo '<tr>';
            echo '<td>ITEMS</td>';
            echo '<td>QUANTITY</td>';
            echo '<td>Infistall Location</td>';
            echo '<td>Infistall Address</td>';
        echo '</tr>';
                while($obj = $result->fetch_object()) {
                echo '<tr>';
                echo '<td>'.$obj->items.'</td>';
                echo '<td>'.$obj->esquantity.'</td>';
                echo '<td>'.$obj->infistall_Location.'</td>';
                echo '<td>'.$obj->infistall_address.'</td>';
                echo '</tr>';
            }
        echo '<tr><td colspan="3"></td><td><input type="submit" /></td></tr>';
} else {
        echo '<tr><td colspan="4">NO Records Found</td></tr>';
    }
echo '</table>';
?>