transform result into an array so -function select php

1

I'm trying to create a function to do select in the Database. But he is now bringing an array into the array. How to leave in an array only. Code I created:

 <?php

include 'conect.php';
function select($con,$tabela,$colunas,$where=1){

    $sql ="SELECT $colunas FROM $tabela WHERE $where";
    $executar= mysqli_query($con,$sql); 
    $colunasdivididas = explode(",", $colunas);
    $numerodecolunas = count($colunasdivididas);
    $numrow= mysqli_num_rows($executar);
    $iii=1;
    $numero=['numeroderetorno'=>2];
    while($row = mysqli_fetch_array($executar)){
        if($colunas!='*' AND $numerodecolunas>0 ){


                for($l=0;$l<$numerodecolunas;$l++){
                        $array[$colunasdivididas[$l].'-'.$iii] = $row[$colunasdivididas[$l]];

                }

        }elseif($colunas=='*'){

        }else{

                $array[$colunasdivididas[$l].'-'.$iii] = $row[$colunasdivididas[$l]];

        }
    $iii++;

    }
    $arraynumrow=['reposta'=>$numrow];
    $arrays=$arraynumrow+$array;

    return $arrays;
}
$ts=selectfuncionarios($link,'dono','id,login,senha');
    print_r($ts);
    ?>

result

    
asked by anonymous 14.10.2018 / 02:56

3 answers

2

I believe this somewhat simplified function should work. I just removed some unnecessary elements and narrowed down an array layer you were inserting:

Array with one less layer:

$array[$colunasdivididas[$l].'-'.++$ii] = $row[$colunasdivididas[$l]];

Simplified PHP Funtion:

function selectfuncionarios($tabela, $colunas = '*', $where = 1) {
    include 'conect.php';

    $sql = "SELECT $colunas FROM $tabela WHERE $where";
    $executar = mysqli_query($con,$sql);
    $colunasdivididas = explode(',', $colunas);

    $ii = 1;
    $numero = ['numeroderetorno' => 2];
    $array = [];
    while ($row = mysqli_fetch_array($executar)) {
        if ($colunas != '*' && count($colunasdivididas) > 0) {
            for ($l = 0; $l < count($colunasdivididas); $l++) {   
                $array[$colunasdivididas[$l].'-'.++$ii] = $row[$colunasdivididas[$l]];
            }            
        } elseif ($colunas=='*') {

        } else {
            $array[$colunas.'-'.++$ii] = $row[$colunas];
        }
    }

    return json_encode($array);
}
    
19.10.2018 / 21:10
-2

Try to put the result in a while. ex:

while($row = mysqli_fetch_array($result)) {             
    $posts['post_id'] = $row['post_id'];
    $posts['post_title'] = $row['post_title'];
    $posts['type'] = $row['type'];
    $posts['author'] = $row['author'];  
} 
    
15.10.2018 / 18:57
-2
$array = array();
while($row = mysqli_fetch_array($executar)){
    if($colunas!='*' AND $numerodecolunas>0 ){


         for($l=0;$l<$numerodecolunas;$l++){
             $array[$ii] = array_push( $array , ($ii=>$row[$colunasdivididas[$l]]));

             $ii++;
         }

    }elseif($colunas=='*'){

    }else{
        $array = array_push( $array , ($colunasdivididas[$l]=>$row[$colunasdivididas[$l]])) );

    }


}
    
15.10.2018 / 19:05