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?
$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<?phptagerror_reporting(E_ALL); ini_set('display_errors', 1);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.if ( $cs === FALSE ) { echo "The search of $name return no result"; } else {doesnt work as it doesn't show me the error message ofThe search of $name return no resultwhen I submit a non database data to query the database. Hope this provide more clearer pictureif($cs != TRUE)but am not 100% sure if that will be the expected result.