transform result into an array so -Function select php

Asked

Viewed 307 times

1

I’m trying to create a Function to do select in the Database. However, it is now bringing an array within the array. How to leave it 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);
    ?>

outworking inserir a descrição da imagem aqui

  • What parameters are you passing to your function at the time of testing?

  • You could copy all the code from the PHP file ?

  • @Ronaldoamaral , that would be all the code because and this Function that I am creating the rest would be just calling it type $call=selectfunctios('table name','column name','condiçaoseouver');

  • @tropicoder12 these parameters, selectfuncionarios('owner','id,login,');

  • 1

    Doesn’t it work if you simply delete an array layer? Ex: $array[$colunasdividedas[$l]. '-'. $iii] = $Row[$colunasdividedas[$l]];

  • Your code has some very complex logics that I’m not sure are necessary.

  • @Thiagoyou, basically what I’m trying to do and a Function that returns in array the select response, and a mode so that you can use select by calling Function and informing the data

  • @Thiagoyou put your answer :"$array[$colunasdividedas[$l]. '-'. $iii] = $Row[$colunasdividedas[$l]];" in the post because it worked obg

  • @Cyberhacker I posted the answer :)

  • In fact, this code is a mess. I think it’s a good idea that the answers refactor and redo it, with a new idea. It makes no sense to call selectfuncionarios and still have to pass the table name by parameter. Not counting the lack of indentation and a for within a while. If I have time, I can help you work something out.

  • @Wallacemaxters, like I said I’m new in php, I would love if you have a while to help me improve, the Omo I gave from Function was just to stay obivio that she facia kkk

  • @Wallacemaxters, my idea was to avoid having to rethink the parameters 'mysqli_fetch_array' and others so the person would be able to select in the database just by informing the table the name of the calunas and if a or n such a condition on a line so it will do the select on bank and then and just treat it

Show 7 more comments

3 answers

2


I believe that Function a little more simplified should work. I just removed some unnecessary elements and narrowed down an array layer you were inserting:

Array with one layer less:

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

PHP Funtion Simplified:

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);
}

-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]])) );

    }


}
  • that n interfere, I put patrao ti will stay that if the person n put a condition $sql ="SELECT $columns FROM $table WHERE 1";

  • @Cyberhacker If you want to merge arrays into one, why don’t you use the array_merge()? https://www.w3schools.com/php/func_array_merge.asp

  • I thought about it, however Function select I am doing and to be used anywhere, even though I know how many columns the user is calling and how many times it will go through the wilhe using num_rows, n worked because n accepted for($i=0:$i<$ii;$i+)$ts .= array_merge($array[i]); it ta arayarrayarray

  • @Cyberhacker, what if it is? I edited the answer to use the array_push method, so add the element at the end of the array to each cyclo do for, http://php.net/manual/en/function.array-push.php?

  • I’ll be taking a look thank you

-3

Try putting 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'];  
} 
  • 1

    and how it would bring to Return?

Browser other questions tagged

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