database - mysql PDO how to bind LIKE -


in query

select wrd tablename wrd '$partial%' 

i'm trying bind variable '$partial%' pdo. not sure how works % @ end.

would

select wrd tablename wrd ':partial%' 

where :partial bound $partial="somet"

or

select wrd tablename wrd ':partial' 

where :partial bound $partial="somet%"

or entirely different?

+1 karim's answer covers it. say:

"select wrd tablename wrd concat(:partial, '%')" 

to string joining @ mysql end, not there's particular reason in case.

things bit more tricky if partial wrd looking can contain percent or underscore character (since have special meaning operator) or backslash (which mysql uses layer of escaping in operator — incorrectly, according ansi sql standard).

hopefully doesn't affect you, if need case right, here's messy solution:

$stmt= $db->prepare("select wrd tablename wrd :term escape '+'"); $escaped= str_replace(array('+', '%', '_'), array('++', '+%', '+_'), $var); $stmt->bindparam(':term', $escaped); 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -