Jump to content

[SOLVED] checking to see if a query is empty


jas4

Recommended Posts

basically I am performing a query linked to a database (looking for products in a catalogue) and if there are no products, i want to display a message saying there are no products, but if there are products i want to display them. I have tried a few variations but nothing seems to work.

 

any advice appreciated

 

$query='SELECT * FROM diet_food';

$results=mysql_query($query)

  or die(mysql_error());

 

    iif (!empty($query)){

        while ($row = mysql_fetch_assoc($results)) {

              extract($row);

              echo '<div class="thumbnail">';

              echo " <a href='getproddietfood.php?prodid=" . $id ."'><img src='images/shopItems.gif' alt='' width='65' height='90'></a>";

              echo '    <p><h2>',$name,'</h2>';

              echo'</div>';

      }

          else {echo "Sorry";}   

    }

 

An example.

 

<?php

  $sql  = "SELECT * FROM foo";
  // execute the query.
  if ($result = mysql_query($sql)) {
    // check for results.
    if (mysql_num_rows($result)) {
      while($row = mysql_fetch_assoc($result)) {
        // echo results.
      }
    } else {
      echo "No results found";
    }
  } else {
    echo "Query failed\n$sql\n" . mysql_error();
  }

?>

ok its looking much better now, but still not properly working. I've modified it slightly to fit round my code to this:

$query='SELECT * FROM f_equip';
$results=mysql_query($query)
  or die(mysql_error());
  

  // execute the query.
  if ($results = mysql_query($query)) {
    // check for results.
    if (mysql_num_rows($results)) {
      while($row = mysql_fetch_assoc($results)) {
              extract($row);
              echo '<div class="thumbnail">';
              echo " <a href='getproddietfood.php?prodid=" . $id ."'><img src='images/shopItems.gif' alt='' width='65' height='90'>";
              echo '     <p><h2>',$name,'</h2>';
              echo'</div>';
          }
      }
    } else {
      echo "<div id='noShopItems'><h2><br><br>There are currently no food supplement products in stock, <br>
           please check back later.<h2></div>";
    }
  } else {
    echo "Query failed\n$query\n" . mysql_error();
  }

 

if there are no products then it works fine, however if there are products found there is nothing displayed.

 

thanks for that first reply

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.