PDO : prepare with bindvalue and like %

Jérôme B

I've looked over an hour on various website but I couldn't solve my problem.

So here is the code that works:

$animes = array();
    $q = $this->_db->query('SELECT id, nom, nom_id FROM animes WHERE nom LIKE "%code%"');
    while ($data = $q->fetch(PDO::FETCH_ASSOC))
    {
        $animes[] = new Anime($data);
    }
    return $animes;

And here is the one that doesn't work :

$animes = array();
$q = $this->_db->prepare('SELECT id, nom, nom_id FROM animes WHERE nom LIKE :n');
$q->bindValue(':n',"%code%",PDO::PARAM_STR);
    while ($data = $q->fetch(PDO::FETCH_ASSOC))
     {
         $animes[] = new Anime($data);
     }
return $animes;`

I use %code% in this example but it will be used with $info which is a $_POST value that I retrieve.

How can I solve it ?

Thank you.

Awlad Liton

you did not execute().

after binding you need to execute then fetch:

$q->bindValue(':n',"%code%",PDO::PARAM_STR);
$q->execute();  
while ($data = $q->fetch(PDO::FETCH_ASSOC))

you can bind like this with php variable:

$q->bindValue(':n','%'.$var.'%',PDO::PARAM_STR);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Illegal mix of collations for operation 'like' with PDO bindValue()

From Dev

PDO bindValue for table and column

From Dev

PDO binding with bindValue not working

From Dev

PDO bindValue an empty string in codeigniter

From Dev

BindValue Loop in insert wih pdo

From Dev

PDO (bindValue) not liking my wildcards

From Dev

PDO query method with bindValue() seems not to be working

From Dev

PDO SQLSRV Using bindValue for SELECT TOP

From Dev

PDO bindValue works for other inputs, why not this one?

From Dev

PDO debugging update query using bindValue

From Dev

PDO SQLSRV Using bindValue for SELECT TOP

From Dev

PDO - That big bindValue vs bindParam in my situation

From Dev

CakePHP PDO prepare statement

From Dev

PDO prepare fails

From Dev

"prepare" PDO function error

From Dev

PHP PDO prepare and execute

From Dev

pdo prepare escaping single quotes

From Dev

PDO prepare/execute SQL issue

From Dev

PDO prepare statement search error

From Dev

PHP PDO Multiple Prepare Statement

From Dev

PDO bindValue isn't allowing a specific parameter to pass through (PHP)

From Dev

PHP PDO - Update with bindValue (set and where parametes are same)

From Dev

Error in PDO page Call to a member function bindValue() on a non-object

From Dev

Should I use bindParam(), bindValue(), or execute() for PDO prepared statements

From Dev

Error in PDO page Call to a member function bindValue() on a non-object

From Dev

PHP PDO - Update with bindValue (set and where parametes are same)

From Dev

Formatting LIKE in odbc_prepare

From Dev

INSERT into DB table with PDO prepare and bindParam

From Dev

"Undefined method PDO::execute()" despite using prepare

Related Related

HotTag

Archive