Hi
i was wondering if anyone had any idea what im doing wrong for the error handling in my script? what im trying to do is if there is no record in the database it will show a message saying no record found when a search is made.

<?php
require("common.php");
$error_message="";
if (isset($_POST['submit'])){
if(empty($_POST['term']))
{
$error_message="Please enter a Ticket Number.";
}
else
{
$query = "SELECT department, subject, message FROM supporttickets Where ticketnumber LIKE :term";
$stmt = $db->prepare($query);
$stmt->execute(array(':term' => $_POST['term']));
while (list($department,$subject,$message) = $stmt->fetch(PDO::FETCH_NUM)) {

///I'M SURE THIS IS WRONG
if($stmt->rowCount()>1)
    echo 'no record found';

else


echo htmlentities($department);

  }
 }
}
?>

    If there are no rows in the result set, fetch() will return FALSE, thus the loop will never be executed. Otherwise, how else would the while() loop ever terminate? Thus, your check to see if any rows are in the result set is in the wrong place.

      bradgrafelman wrote:

      Thus, your check to see if any rows are in the result set is in the wrong place.

      It's also backwards and gives the wrong answer in the case where there is exactly one record returned.

        Write a Reply...