How to select with an array?

Asked

Viewed 5,767 times

0

Good is the following I have an array designated by $posts, this $posts array is an array containing several things, such as: Work, Dinner, House, Outside.

What I want is to select a table in the database Where post=$posts, that is to all results, that are in the array?

How can I do that?

Thank you.

@EDIT:

 echo $postosstring;

    $procura1 = mysqli_query($link, "SELECT * FROM faturacoes where Status = 1 AND Data >= '$datainicial' AND Data <= '$datafinal' AND posto IN ('.$postosstring.')");

    // Total Contratos

    $resultset1 = mysqli_query($link, "SELECT SUM(contratos) as contratos FROM faturacoes where status='1' AND Data >= '$datainicial' AND Data <= '$datafinal' AND posto IN ('.$postosstring.')"); 

    $linha1= mysqli_fetch_assoc($resultset1); 

    $soma1 = $linha1['contratos'];

    // Total Valor
$resultset2 = mysqli_query($link, "SELECT SUM(valor) as valor FROM faturacoes where Status = 1 AND Data >= '$datainicial' AND Data <= '$datafinal' AND posto IN ('.$postosstring.')");


$linha2= mysqli_fetch_assoc($resultset2); 

$soma2 = $linha2['valor'];

My SELECT is not working.

  • Explain this better.

  • I intend to do a select, where Where is an array, basically that’s it.

  • But what’s in that array? A list of the same field? if applicable, you can use WHERE IN.

  • I’ll try the Where in.

  • I think I got it wrong, it seems you want to create a query based on an array. If this is simple, it can be done with a simple iteration(foreach). Put your PHP code, if possible.

1 answer

7


You can use the IN.

If the array is:

$array = ['Trabalho', 'Jantar', 'Casa', 'Fora'];

You need to pass this to a string, in order to stay:

"Trabalho","Jantar","Casa","Fora"

That way you can use IN("Trabalho","Jantar","Casa","Fora").

Whereas you are not using bind_param!

For this you have several options, which achieve the same goal:

You can use the implode:

$string = '"' . implode('","', $array) . '"';
// Resultado: "Trabalho","Jantar","Casa","Fora"

If you want you can make a loop or an array_map maybe.

foreach($array as $i => $coisa){

   $string .= '"';
   $string .= mysqli_real_escape_string($sql, $coisa);
   $string .= '"';
   $string .= $i + 1 < count($array) ? "," : "";

}

Then insert into SQL:

$query = mysqli_query($sql, 'SELECT * FROM Tabela WHERE coisa IN ('.$string.')');

You can add other parameters in the WHERE normally.

mysqli_query($link, 'SELECT * FROM tabela where status = "1" AND Data >= "'.$datainicial.'" AND Data <= "'.$datafinal.'" AND coisa IN ('.$string.')'); 

If you prefer to use double quotes " should remove the ':

mysqli_query($link, "SELECT SUM(valor) as valor FROM faturacoes where Status = 1 AND Data >= '$datainicial' AND Data <= '$datafinal' AND posto IN ($postosstring)");

Your current code with the ('.$postosstring.') is doing that: '"Trabalho","Jantar","Casa","Fora"' instead "Trabalho","Jantar","Casa","Fora".

  • It’s not working Show me nothing.

  • I want to add + with AND and it doesn’t work.

  • Apparently the problem is that it’s using IN ('.$postosstring.')" the correct is without the '. Exchange for IN ($postosstring)". This will have the same effect as the code above.

  • Try to use the mysqli_error($link) after the first SELECT to see what is happening.

  • Perfect! This is just what I needed.

Browser other questions tagged

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