#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Stmt->errorCode not working


    hi
    im having a little problem getting the if($stmt->errorCode() == 0) { in my code to work. If i do a search with a ticket number that is in the database it will show it but if i do a search with a random number thats not in the database it will not show the error message no ticket found.
    PHP Code:
    <?php
    require("db.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']));

    if(
    $stmt->errorCode() == 0) {
    while (list(
    $department,$subject,$message) = $stmt->fetch(PDO::FETCH_NUM)) {
    echo 
    htmlentities($department);
    }
    }else{

    $error_message="no ticket found.";
     }

     }
     }

    ?>
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    Running a query that returns no results is not considered to be an error by the database system and won't generate any error codes or error messages.

    You can call the rowCount() method to determine how many rows were returned by the query.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo