5
I have that code:
$whereBusca = "itaim-bibi";
$whereBusca = explode(',', $whereBusca);
$sql = $pdo->prepare('SELECT id, nome, caminho FROM regiao WHERE caminho IN (:whereBusca)');
$sql->execute(array("whereBusca" => "'".implode("','", $whereBusca)."'"));
$resultadoSql = $sql->fetchAll();
foreach ($resultadoSql as $valorSql) {
echo utf8_encode($valorSql['id']);
}
It works well, but if the $whereBusca content is more than a region, it doesn’t give the search, I don’t know what I’m missing, look at the example of when it doesn’t work:
$whereBusca = "itaim-bibi,jardins";
$whereBusca = explode(',', $whereBusca);
$sql = $pdo->prepare('SELECT id, nome, caminho FROM regiao WHERE caminho IN (:whereBusca)');
$sql->execute(array("whereBusca" => "'".implode("','", $whereBusca)."'"));
$resultadoSql = $sql->fetchAll();
foreach ($resultadoSql as $valorSql) {
echo utf8_encode($valorSql['id']);
}
See if quotation marks change anything:
array("whereBusca" => "'".implode("','", $whereBusca)."'" )
– Bacco
Hi, also not.... until I understood what you thought, to be like this right? " 'Taim-Bibi','Jardin'" ...
– caiocafardo
In fact it was the only thing I thought, to give explode and then implode. I do not know if in this case PREPARE is the best way to solve the problem. I’ve been posting some answers on the site that make a more complex WHERE, with a LIKE '%' for each term, maybe it’s nice you take a peek. (maybe it’s too much). The ideal would be to give a print_r in strategic places.
– Bacco
See if any help, there are some 3 similar versions lost here: http://answall.com/search?q=user%3A70+Where+like
– Bacco
It may be that the PDO is sanitizing the quotes (most likely). It would be the case to mount the string manually even. The problem is that you would have to avoid sql Injection on your own, in case.
– Bacco
Vo already leave in the question with the quotation marks of the way you spoke, which I think is the "correct", as it would be this manual form?
– caiocafardo
You cannot pass 1 placeholder and N values, need to be in the same ratio or be 1 placeholder for 1 value. How to use PDO bindParam in query IN()?
– rray
This field has several values separated by commas ?
– rray
What fields? From the database?
– caiocafardo
Apparently that’s what you said, thanks, now I fight here to make the loop for each query value... Ta dureza!
– caiocafardo
And if you do so: 'SELECT id, name, path FROM regiao WHERE PATH IN ("Itaim-Bibi","gardens")'
– Ivan Nack
Managed to hit the
in()
what’s the problem now?– rray
@rray is that I am not the biggest expert in the world, I try to use the example that you passed, but ta crap....
– caiocafardo
$whereBusca
is an array right? and the number of elements may vary– rray
This, from 1 to 400 (400 regions in the database), comes as pq string picked via GET, but turns into array with explode
– caiocafardo