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";}   

    }

 

Link to comment
Share on other sites

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();
  }

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.