I am trying to get a search that searches through SQL and brings back results and display no match found if there were no matches. I have tried using an if statement but it does not seem to work. When I type something I know that is not in the database, it does not print out not found and does nothing. I was thinking maybe a for statement but I don't know how using PDO.
Also, can I get help for modifying the code to prevent SQL injection. Thanks.
if(isset($_POST['find'])) {
$sq = $_POST['find'];
$q = $db->prepare("SELECT * FROM table WHERE name LIKE '%$sq%'");
$q->execute();
if (!$q->rowCount() == 1 ) {
while ($r = $q->fetch(PDO::FETCH_ASSOC)) {
print $r['datadb'] . "<br />\n";
}
} else {
print 'Not found';
}
}