-3

I make an explode inside a while to separate words with a comma (,) and fix it so that it can be put in a sql query, in the first sample of the while it correctly outputs what I want but in the second iteration of the while it shows the words of the previous output:

$queryselectprocesses ="SELECT * FROM laziness_config_processes WHERE busid = $buss ";
$execqueryselectprocesses = sqlsrv_query($con_laziness, $queryselectprocesses);

while($fila = sqlsrv_fetch_array($execqueryselectprocesses,SQLSRV_FETCH_ASSOC)){
$modalities_select = $fila['modalities'];
$institutions_select = $fila['institutions'];

#the result in the first output of the while of $modalities_select = AB, CD

#the result in the first while output of $institutions_select = CAR, MOTORCYCLE

#the result in the second output of the while of $modalities_select = FG, HI

#the result in the second while output of $institutions_select = BIKE, HELMET

$queryselectprocesses ="SELECT * FROM laziness_config_processes WHERE busid = $buss ";
$execqueryselectprocesses = sqlsrv_query($con_laziness, $queryselectprocesses);

while($fila = sqlsrv_fetch_array($execqueryselectprocesses,SQLSRV_FETCH_ASSOC)){
$modalities_select = $fila['modalities'];
$institutions_select = $fila['institutions'];

$mods = explode(',', $modalities_select);
for ($i = 0; $i < count($mods); $i++){
    $mods[$i] = trim($mods[$i]);
}
foreach ($mods as $value) {
    if ($value != '-1' and trim($value) != '') {
        $mod .= "'" . trim($value). "',";
    }
}
if (trim($mod) != '') {
   $mod = substr($mod, 0, strlen($mod)-1);
   $mod = " modality in ( $mod ) ";
}

$insts = explode(',', $institutions_select);
for ($i = 0; $i < count($insts); $i++){
    $insts[$i] = trim($insts[$i]);
}
foreach ($insts as $value) {
        if ($value != '-1' and trim($value) != '') {
         $inst .= "'" . trim($value). "',";
    }
}
if (trim($inst) != '') {
      $inst = substr($inst, 0, strlen($inst)-1);
      $inst = " institution in ( $inst ) ";
}

$query = " select * from tb_model where $mod and $inst ";
echo $query;

}

#The result in the first while output of $query = select * from tb_study where modality in ( 'AB','CD' ) and institution in ( 'CAR','MOTO' )

#BUT the result in the second output of the while of $query = select * from tb_study where modality in ( modality in ( 'AB','CD' ) 'FG','HI' ) and institution in ( institution in ( 'CAR','MOTORCYCLE' ) 'BIKE','HELMET' )

#The result that should be output in the second output of the while of $query = select * from tb_study where modality in ( 'FG','HI' ) and institution in ( 'BICYCLE','HELMET' )

2 Answers2

1

You have to reset your $mod at the beginning of while loop. This code ilustrate your problem.

<?php

$showcase = function($resetMod = false) {
    $step = 0;
    $mod  = '';
    while($step++ < 2) {
        
        if($resetMod) {
            $mod = '';
        }
        
        for($i = 0; $i < 2; $i++) {
            $mod .= $i;
        }
        
        echo $mod . ' ';
    }
};

echo $showcase();
echo "\r\n";
echo $showcase(true);

also you should consider using prepared statements instead of building $query by string interpolation. https://stackoverflow.com/a/24989031/14008536

Boorsuk
  • 291
  • 1
  • 10
  • I had not thought about resetting the value, thank you very much for your help. It was solved by adding it to the beginning of while, like this: while($fila = sqlsrv_fetch_array($execqueryselectprocesses,SQLSRV_FETCH_ASSOC)){ $mod = ''; $inst = ''; But if I will do the prepared statements, thanks friend. – Desarrollo Creo Aug 15 '23 at 04:23
0

It was solved by adding it to the beginning of while, like this:

while($fila = sqlsrv_fetch_array($execqueryselectprocesses,SQLSRV_FETCH_ASSOC)){
    $mod = '';
    $inst = '';

But if I will do the prepared statements, thanks friend.