0

Hi i have done research on this topic i did come across a few solutions, although i wasn't able to implement them to my code because i am a beginner to this. My question is basically how can i display a message if value is not found in the MySQL database?

previously searched: Displaying message when no results found in PHP MySQL search and mysql fetch array if no results display message

       <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
 <?php
 $customer = $_GET["custID"];
 $conn = mysql_connect("localhost", "localuser", "pass");
mysql_select_db("testdb", $conn)
or die ('Database not found ' . mysql_error() );
$sql = "SELECT orderNumber, customerID, orderDate, shippingDate, shipped FROM orders where customerID = $customer ORDER by orderDate";
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
?>
<table border="1" summary="Customer Details">
<tr>
<th>Order Number</th>
<th>Customer ID</th>
<th>Order Date</th>
<th>Shipping Date</th>
<th>Shipped</th>
</tr>
<?php
        $results = mysql_fetch_array($rs);
        if ( $results === FALSE )
        {
             echo "No result";
        }
        else
        {
             foreach($results as $item)
             {?>
                <tr>
                    <td><?php echo $result["orderNumber"]?></td>
                    <td><?php echo $result["customerID"]?></td>
                    <td><?php echo $result["orderDate"]?></td>
                    <td><?php echo $result["shippingDate"]?></td>
                    <td><?php echo $result["shipped"]?></td>
                </tr>
           <?php  }
        }
mysql_close($conn); ?>
</table>
</body>
</html>
2
  • So you already found a couple of answers and, since you don't understand them, you're asking for a third one? This process will take forever. Don't you think you should try to understand the answers you have so far? Commented Apr 27, 2013 at 8:51
  • BTW, you'd better finish your project quickly. The mysql extension will throw deprecated warnings in PHP 5.5 and will probably be removed in PHP 5.6. Commented Apr 27, 2013 at 8:53

3 Answers 3

0

Use mysql_num_rows()

if(mysql_num_rows($rs) > 0) {
  // got records
}
else {
  // no records found
}

Note:

Don't use mysql_* family functions because they are going to deprecated. Start to look into mysqli or pdo

Sign up to request clarification or add additional context in comments.

1 Comment

Updated the code although when correct value is entered no data is displayed, any ideas why?
0
$result = mysql_fetch_array($rs)
if($result)

...

Comments

0

This usage if(count($result > 0)) is wrong. mysql_fetch_row always returns FALSE when there is no result. Please see: http://php.net/manual/en/function.mysql-fetch-array.php

You should use sometime like that

    <?php
        $results = mysql_fetch_array($rs);
        if ( $results === FALSE )
        {
             echo "No result";
        }
        else
        {
             foreach($results as $item)
             {
                     ...

             }
        }
    ?>

3 Comments

I did try your code as well same thing happens as i mentioned with the code i tried from @GBD the no result found works if no value was returned. Although when a correct value is entered no data is populated within the table, so just stuck on that bit.
Updated code again, this time error occurs.
Error about query being incorrect.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.