How to save array of select Mariadb and save values in different variables (not the complete array) PHP?

0

Hello, I have a function that returns an array but I want to access by means of its indexes to the value that I want from the selected fields in the database, for example, to save the variable at the index [0] in other words the first value of the arrangement and in the variable b save the index [1] and so on. This is my code I thought working with arrays was a matter of using indexes.

class Tabla{
    public function salida_materiales() {
            try {
                $orden_de_compra = $_GET['orden_de_compra'];

                include("resource/Database.php");
                $sql = "SELECT folio, fecha, numero_remision ,numero_factura,otro,referencia_transporte ,placa_tractor,placa_caja,comentarios,retorno,
    fecha_posible  FROM salida_materiales WHERE orden_de_compra= :orden_de_compra";
                        $query = $db->prepare($sql);
                            $query->bindparam(':orden_de_compra', $orden_de_compra);




                $query->execute();
                $tabla = $query->fetchAll(PDO::FETCH_ASSOC);
            } catch (PDOException $e) {
                //echo "Exeption: " .$e->getMessage();
                $result = false;
            }
            $query = null;
            $db = null;
            return $tabla;        
        }

}

I call the function and it does not save the value

 $tabla = new Tabla();
    $arreglo = $tabla->salida_materiales();
    $folio = $arreglo[0];

and so if I wanted more variables from the array obtained by the query, by the way I already protected my code against the sql injection thanks to the recommendation of a user of this page.

    
asked by Daniel Treviño 13.09.2017 в 21:19
source

2 answers

1

This can help you:

  

PDO :: FETCH_ASSOC: returns an array indexed by the names of the   columns of the result set.

     

PDO :: FETCH_BOTH (default): returns an array indexed by both   column name, as numerically with base index 0 such as   was returned in the result set.

     

PDO :: FETCH_NUM: returns an array indexed by the column number   As it was returned in the result set, starting with the   column 0.

Use FETCH_BOTH or FETCH_NUM.

    
answered by 13.09.2017 / 21:39
source
1

Since you used PDO :: FETCH_ASSOC, the result will index you according to the name of the columns. Also, you will have one of those arrangements for each row of the result.

So the correct way to read the folio column is:

$tabla = new Tabla();
$arreglo = $tabla->salida_materiales();
$folio = $arreglo[0]['folio']; // primer folio encontrado.

You can also print the result to see the structure: print_r($arreglo);

    
answered by 14.09.2017 в 02:29