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);
?>
What parameters are you passing to your function at the time of testing?
– adrianosmateus
You could copy all the code from the PHP file ?
– Ronaldo Amaral
@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');
– Cyber Hacker
@tropicoder12 these parameters, selectfuncionarios('owner','id,login,');
– Cyber Hacker
Doesn’t it work if you simply delete an array layer? Ex: $array[$colunasdividedas[$l]. '-'. $iii] = $Row[$colunasdividedas[$l]];
– ThiagoYou
Your code has some very complex logics that I’m not sure are necessary.
– ThiagoYou
@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
– Cyber Hacker
@Thiagoyou put your answer :"$array[$colunasdividedas[$l]. '-'. $iii] = $Row[$colunasdividedas[$l]];" in the post because it worked obg
– Cyber Hacker
@Cyberhacker I posted the answer :)
– ThiagoYou
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 afor
within awhile
. If I have time, I can help you work something out.– Wallace Maxters
@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
– Cyber Hacker
@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
– Cyber Hacker