How to turn values from an array into PHP variables for BD query?

Asked

Viewed 43 times

0

I am in dire need of any help from you with a little problem in PHP/Mysql.

I have a variable that has the names of medicines separated by commas (string), the amount of names stored in this variable can always vary, e.g.:

$medicamentos = "remedio1, remedio2, remedio3";

I take this string and separate the meds by exploding the commas:

$arrayMed = explode(", ",$medicamentos);

So far so good, but how I get these medicines already separated coming from this array so I can make a query in the BD as in the example below?:

$ReadMedicamentos = ExeRead("tabela_medicamentos", "WHERE medicamento_nome = '$variavel_vinda_do_array'");
while ($m = mysqli_fetch_assoc($ReadMedicamentos)):  
   extract($m);
endwhile;

I need all medicines in this array to be searched in the BD.

Note: I use the "Exeread" function to make queries in BD using Mysqli.

How can I make this consultation in BD for all medications that are present in Array?

From now on I appreciate any help.

  • 1

    Using your example $arrayMed = explode(", ",$medicamentos); then $arrayMed[0] === remedio1, $arrayMed[1] === remedio2 and $arrayMed[2] === remedio3. Then do foreach ($arrayMed as $remedio) { sua consulta }

  • 1

    So this "$remedio" variable that you put there, you’re going to get every turn of the foreach loop the name of a drug, this? And in this case this variable would take the place of my variable '$variavel_vinda_do_array" that I used in the example, this?

  • And in this case, I can use this variable "$remedio" instead of my variable ''$variavel_vinda_do_array" that I used in the example?

  • 1

    Exactly, this loop was built for this.

  • Excellent @Augustovasques, very good indeed. .

No answers

Browser other questions tagged

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