How to pass information in bindvalue with two or more information stored in the variable?

Asked

Viewed 37 times

1

I’m having a problem... I’m getting some id from the database and storing in $ids_cozinha. gave a var_dump and a print_r, is returning me the id I want, so far so good.

<pre>
<?php print_r(implode(", ",$ids_cozinha))?>
</pre>

I have that result.

20, 21

I have a query on php prepare("SELECT XXXXX") that I need to enter these ids (21 and 21) that were stored in $ids_kitchen, but bindValue is not accepting more than an id from the variable $ids_cozinha, precisely because it is an array.

It’s like this..

$sql->bindValue(":ids_cozinha", implode(", ",$ids_cozinha));

They’re still not accepting the ids. It is only finding the first ID that is the 20.

How can I pass these ids that are storing in this $ids_cozinha?

PS: Running the query directly in the database, are returning with accuracy all the information I need.

  • Welcome Raphael, it would be possible to post the entire code, it’s kind of hard to understand by these fragments...

  • 1

    Hello Alvaro, thanks for helping, if I post the whole code can end up getting confused even more, given that my query is a little large, because I do LEFT JOIN in several tables to insert the result in :ids_cozinha. Above this code I select and store these ids (array) in a $variable. So I do this. $ids_kitchen = $variable As

1 answer

0

I was going through a similar situation to that question. I was wanting to take the emails of users with the ids that were in that '$array'.

I solved it the way below:

$array = [1,5,9];
$ids = implode(',',$array);
                           
 $conn = new Conexao();
 $conexao = $conn->conectar();
                                            
 $query = "SELECT email FROM usuarios WHERE id IN(".$ids.")";
                            
 $stmt = $conexao->prepare($query);                           
 $stmt->execute();                            
 $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

There is also the alternative form below (who recommended me and said to be better):

 $conn = new Conexao();
 $conexao = $conn->conectar();

 $array = [1,5,9];
 $param = rtrim(str_repeat('?,', count($array)),',');
                                            
 $query = "SELECT email FROM usuarios WHERE id IN(".$param.")";
                            
 $stmt = $conexao->prepare($query);                           
 $stmt->execute($array); 

 $result = $stmt->fetchAll(PDO::FETCH_ASSOC); 

 

Thus above the values are not passed directly to the query.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.