Warning: Invalid argument supplied for foreach () PDO / SQLServer

0

Greetings, I have a code that is giving me problems with a foreach and I have not achieved its correct operation, it is a query to a database SQLServer with PDO ODBC, I have tested the queries separately in PHP and it does not give problems (These queries must be dynamic has resulted from the results of a static), I have tested the queries directly in the database engine and the results are the expected the problem arises in the php, I hope you can help me.

PHP

function cargosUsuarios(){

    $mssql=conectarBDSOF();
    $sql="SELECT codiCC, DescCC FROM v_TPersonalB WHERE codiCC LIKE '20%' AND mes<>'' GROUP BY DescCC, codiCC";
    $cargosNo=array('002','006','009','010','027','67','001','021','016','011','005','038','054','76','003','007','62','81');
    $tablaCC='<table class="table" style="margin-bottom:0;">';
    $tablaCC.='<tbody>';
    foreach ($mssql->query($sql) as $row) {

        $sqlCARGOS="SELECT CarNom, carCod FROM v_TPersonalB WHERE codiCC='".trim($row["codiCC"])."' AND mes<>'' AND carCod<>'' GROUP BY CarNom, carCod";
        if("40-04"!=$row["codiCC"]){
                $cargos='<table class="table" style="margin-bottom:0;">';
                $cargos.='<tbody>';
                echo $sqlCARGOS;
                foreach ($mssql->query($sqlCARGOS) as $row2) {  // AQUI SURGE EL PROBLEMA           
                    if(in_array($row2["carCod"], $cargosNo)){}else{
                        $cargos.='<tr>';
                        $cargos.='<td>'.$row2["CarNom"].'</td>';
                        $cargos.='<td><input type="checkbox" name="cargosUsusarios[]" value="'.$row["codiCC"].'/'.$row2["carCod"].'"></td>';
                        $cargos.='</tr>';
                    }                   
                }
                $cargos.='</tbody>';
                $cargos.='</table>';

                $tablaCC.='<tr>';
                $tablaCC.='<td style="vertical-align:middle;">'.$row["DescCC"].'</td>';
                $tablaCC.='<td colspan="2">'.$cargos.'</td>';
                $tablaCC.='</tr>';
        }
    }
    $tablaCC.='</tbody>';
    $tablaCC.='</table>';
    echo $tablaCC;  
}
    
asked by Kevin 28.12.2017 в 14:59
source

1 answer

0

The error I found using

try{
$coneccion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
//codigo
}catch(PDOException $e){
    echo $e->getMessage();
}
  

The connection is busy with the results of another hstmt

so the solution that occurred to me (I do not know if there is another way) was to create 2 array()

$codiCC=array();
$DescCC=array(); 

which I fill in the first foreach

$i=1;
foreach ($mssql->query($sql2) as $row) {
    $codiCC[$i]=$row["codiCC"];
    $DescCC[$i]=$row["DescCC"];
    $i++;
}

in this way I release the connection, then I go through the array and perform the dynamic queries to the bd

foreach ($codiCC as $key => $value) {//Recorro un array

        $sqlCARGOS="SELECT CarNom, carCod FROM v_TPersonalB WHERE codiCC='".trim($value)."' AND mes<>'' AND carCod<>'' GROUP BY CarNom, carCod";
        if("40-04"!=$value){
                $cargos='<table class="table" style="margin-bottom:0;">';
                $cargos.='<tbody>';
                foreach ($mssql->query($sqlCARGOS) as $row2) {              
                    if(in_array($row2["carCod"], $cargosNo)){}else{
                        $cargos.='<tr>';
                        $cargos.='<td>'.$row2["CarNom"].'</td>';
                        $cargos.='<td><input type="checkbox" name="cargosUsusarios[]" value="'.$value.'/'.$row2["carCod"].'"></td>';
                        $cargos.='</tr>';
                    }                   
                }
                $cargos.='</tbody>';
                $cargos.='</table>';

                $tablaCC.='<tr>';
                $tablaCC.='<td style="vertical-align:middle;">'.$DescCC[$key].'</td>';
                $tablaCC.='<td colspan="2">'.$cargos.'</td>';
                $tablaCC.='</tr>';
        }
    }

Having made these modifications the final code and get the desired result is:

$mssql=conectarBDSOF();
    $sql="SELECT codiCC, DescCC FROM v_TPersonalB WHERE codiCC LIKE '20%' AND mes<>'' GROUP BY DescCC, codiCC";
    $sql1="SELECT codiCC, DescCC FROM v_TPersonalB WHERE codiCC LIKE '40%' AND mes<>'' GROUP BY DescCC, codiCC";
    $sql2="SELECT codiCC, DescCC FROM v_TPersonalB WHERE codiCC LIKE '10%' AND mes<>'' GROUP BY DescCC, codiCC";
    $cargosNo=array('002','006','009','010','027','67','001','021','016','011','005','038','054','76','003','007','62','81');
    $cuerpoProcesos="";
    $cuerpoAdministrativos="";
    $cuerpoProduccion="";
    $tablaCC='<table class="table" style="margin-bottom:0;">';
    $tablaCC.='<tbody>';
    // MODIFICADO
    $codiCC=array();
    $DescCC=array();
    $i=1;
    foreach ($mssql->query($sql2) as $row) {// LLENO LOS ARRAY
        $codiCC[$i]=$row["codiCC"];
        $DescCC[$i]=$row["DescCC"];
        $i++;
    }
    foreach ($codiCC as $key => $value) { // RECORRO LOS ARRAY

        $sqlCARGOS="SELECT CarNom, carCod FROM v_TPersonalB WHERE codiCC='".trim($value)."' AND mes<>'' AND carCod<>'' GROUP BY CarNom, carCod";
        if("40-04"!=$value){
                $cargos='<table class="table" style="margin-bottom:0;">';
                $cargos.='<tbody>';
                foreach ($mssql->query($sqlCARGOS) as $row2) {              
                    if(in_array($row2["carCod"], $cargosNo)){}else{
                        $cargos.='<tr>';
                        $cargos.='<td>'.$row2["CarNom"].'</td>';
                        $cargos.='<td><input type="checkbox" name="cargosUsusarios[]" value="'.$value.'/'.$row2["carCod"].'"></td>';
                        $cargos.='</tr>';
                    }                   
                }
                $cargos.='</tbody>';
                $cargos.='</table>';

                $tablaCC.='<tr>';
                $tablaCC.='<td style="vertical-align:middle;">'.$DescCC[$key].'</td>';
                $tablaCC.='<td colspan="2">'.$cargos.'</td>';
                $tablaCC.='</tr>';
        }
    }
    // FIN DE MODIFICACIONES
    $tablaCC.='</tbody>';
    $tablaCC.='</table>';
    echo $tablaCC;
    
answered by 28.12.2017 / 16:06
source