Saturday, 28 September 2013

Issue with PDO select query: unknown error

Issue with PDO select query: unknown error

I'm trying to get comfortable with PDO but can't get this to work.
Below is a script for a basic search box:
<?php
$sth= new connection();
if (isset($_GET['search'])) {
$search_query = $_GET['search'];
$search_query = htmlentities($search_query)
$result=$sth->con->prepare("SELECT firstname, lastname FROM users WHERE
firstname LIKE '%" . $search_query . "%' OR
lastname LIKE '%" . $search_query . "%' OR
LIMIT 25");
$result->bindParam(1, $search_query, PDO::PARAM_STR, 12);
foreach ($result as $row) {
$firstname = $row["firstname"];
$lastname = $row["lastname"];
if (!($result) == 0) {
?>
<div="foo">Here are your results:</div>
<?php
} else {
?>
<div="bar">No results!</div>
<?php
}
}
?>
Here's the error that I get:
fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[]:
<<Unknown error>>
What am I doing wrong ?
ps: $sth works fine with other queries.

No comments:

Post a Comment