2
I am running a SELECT in the database through a function and returning the result as a multidimensional array, where each primary index refers to a record, and the secondary indices are the database fields with the values.
Below is the assembly of the function return:
$resultado = $this->conn->query($sql);
if ($resultado->rowCount() > 0) {
foreach($resultado as $chave => $valor){
$retorno[$chave] = $valor;
}
return $retorno;
} else {
return false;
}
When I take the return and execute one var_dump()
, the result comes out like this:
array (size=3)
0 =>
array (size=4)
'usu_id' => string '1' (length=1)
0 => string '1' (length=1)
'usu_nome' => string 'Administrador' (length=13)
1 => string 'Administrador' (length=13)
The second dimension of the array is creating two indexes for the return: "0" and "usu_id", as well as "1" and "usu_name".
Is that correct? PHP does it anyway or is there an error in the way I’m assembling the return array?
It is not duplicated, when doing Fetch it is necessary to inform the type of result. You are using PDO, Mysqli or what to connect to the database?
– Sveen
In PDO I use $stmt->fetch(PDO::FETCH_ASSOC);, where it only returns by attributes by key
– Sveen
pq needs the foreach?
– rray
@Sveen - I am using PDO with Mysql database.
– Rodrigo Tognin
@rray - My idea is to assemble an array with the return of the database and return that array, instead of returning the return object (Pdostatement)
– Rodrigo Tognin