PDO Suggest refine results different than wildcard symbol

时间:2015-07-31 20:52:47

标签: php mysql pdo

I am trying to make a PDO query to be searchable not only by the whole string but also by first letter or last letter anything like this. My question is what approach I have to take to achieve this goal.

My original idea was to use wildcard symbol and something like the following:

SELECT * FROM idname WHERE field LIKE CONCAT('%', :field , '%')

but this option for me is not working since I am getting an error:

Warning: Division by zero in**

Warning: Division by zero in E:\xampp\htdocs\search-contact.php on line 111

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 30' at line 1' in E:\xampp\htdocs\dolche\admin\class\pagination.php:451 Stack trace: #0 E:\xampp\htdocs\dolche\admin\class\pagination.php(451): PDOStatement->execute() #1 E:\xampp\htdocs\search-contact.php(125): pagination->execute() #2 {main} thrown in E:\xampp\htdocs\class\pagination.php on line 451

My code ad the moment is the following:

try
{

$paginate = new pagination($page, 'SELECT * FROM idname WHERE field LIKE :field', $options);


}
catch(paginationException $e)
{
    echo $e;
    exit();
}

$paginate->bindValue(':field', $_POST['field'] , PDO::PARAM_STR);
$paginate->execute();

Any suggestions are welcome ?

1 个答案:

答案 0 :(得分:1)

使用PDO时,您必须将通配符放在参数中,因此它看起来像:

$paginate->bindValue(':field', '%'.$_POST['field'].'%' , PDO::PARAM_STR);