Jump to content

[Resolvido] LIKE em PHP Data Objects


Recommended Posts

Posted

Como devo fazer para fazer bind de um parâmetro em PDO e aplicar o SQL(LIKE).

Ex.

public function search_character($string)
 {
 $init = module::tga();
 $q0 = "SELECT name FROM  WHERE name LIKE %:charname%";
 $t1 = $init->prepare($q0);
 $t1->bindParam(":charname", $string, PDO::PARAM_STR);
}

Porém ele retorna um erro de sintaxe, alguém tem uma opinião/solução?

Posted

ao fazeres dessa forma o SQL resultante será, (exemplo com $string = "string" :

SELECT name FROM  WHERE name LIKE %'string'%

como vês, o erro é claro.

necessitas que os caracteres '%' façam parte da string do parâmetro :

public function search_character($string)
 {
 $init = module::tga();
 $q0 = "SELECT name FROM  WHERE name LIKE :charname";          // <-------
 $t1 = $init->prepare($q0);
 $t1->bindParam(":charname", '%'.$string.'%', PDO::PARAM_STR); // <-------
}
IRC : sim, é algo que ainda existe >> #p@p
Posted (edited)

Obrigado pela resposta em curto prazo. Eu até pensei nessa solução mas pareceu ser me um pouco arcaíca, julguei haver já um método próprio da extensão PDO para realizar esse tipo de query.

Entretanto será:

$string = '%'.$string.'%';
Edited by munkbozz

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.