1

Referring to this topic: Display error message if value not found mysql,

I tried to understand what's the correct method to achieved it, my current code show's me the result from database query but when I put some value that not in database, it just doesn't show the error message or any php coding error.

Here is my php code:

error_reporting(E_ALL); ini_set('display_errors', 1);
require_once 'dbconnect.php';

$name = $_POST['name'];

try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "SELECT *
              FROM customer
              WHERE lname  LIKE :name OR
                    fname LIKE :name OR
                    number LIKE :name";

$q = $conn->prepare($sql);
    $q->execute(array('name' => $name));
    $q->setFetchMode(PDO::FETCH_ASSOC);

} catch (PDOException $pe) {
    die("Could not connect to the database $dbname :" . $pe->getMessage());
}

and here is my html view:

            <tbody>
            <?php
            $cs = $q->fetchAll();
            if ( $cs === FALSE ) {
                 echo "The search of $name return no result";
            } else {
            foreach($cs as $r): ?>
            <tr>
                <td><a href="profile.php?name=<?php echo htmlspecialchars($r['slug']) ?>"><?php echo htmlspecialchars($r['fname']), ' ', htmlspecialchars($r['lname']) ?></a></td>
                <td><?php echo htmlspecialchars($r['number']) ?></td>
                <td><a href="profile.php?name=<?php echo htmlspecialchars($r['slug']) ?>">View Profile</a></td>
            </tr>
            <?php endforeach;
            } ?>
        </tbody> 

Kindly advise where did I go wrong?

5
  • Add $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after the connection is opened. Plus, add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); Commented Sep 7, 2014 at 13:48
  • @Fred-ii- added but not working. I suspect html part doesn't working maybe I putted a wrong if else statement? Commented Sep 7, 2014 at 13:52
  • Hm... well I see href="profile.php?name= and $name = $_POST['name']; try $name = $_GET['name']; yet, I'm unsure what the question is about. Try that and see. ?name= is a GET method. Commented Sep 7, 2014 at 13:57
  • hi @Fred-ii-, this part if ( $cs === FALSE ) { echo "The search of $name return no result"; } else { doesnt work as it doesn't show me the error message of The search of $name return no result when I submit a non database data to query the database. Hope this provide more clearer picture Commented Sep 7, 2014 at 14:12
  • 1
    Have a look at Fly's answer below, it may explain why. You can also try if($cs != TRUE) but am not 100% sure if that will be the expected result. Commented Sep 7, 2014 at 14:13

1 Answer 1

2

Technically, nothing is failing with the query itself, so fetchAll() won't return false. What it will do, when you run a query that returns no results, is return an empty array.

In this case, you can alter your if statement to read: if ($cs === false || empty($cs)) {. Alternatively, you can use if (!$cs) { to allow PHP to interpret falsey values, like an empty array.

Source: PDOStatement::fetchAll()

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.