how to convert the results of an array of multiple rows from MySQL into PHP variables

1

Dear in advance I appreciate your contributions, I am stuck in a logic that probably for many of you is quite simple and is the following: I have a table in MySQL and from it I get a number of records associated with the identification of the user like this:

Now, what I want to do is get the results of the column "credit_scale" to save them in PHP variables since I have to perform some operations on them, however, when making a query prepared using FetchAll I get the whole array but not I have the logic to interact with the array in such a way that I can obtain these results in the form of independent variables, as an example what I would like to obtain is the following:

$var1=saldo_credito[0];
$var2=saldo_credito[1];
$var3=saldo_credito[2];

Once again all your contributions are welcome and grateful of me. Greetings

Annex the portion of code that I am using to get to obtain the required result:

<?php
$ob1="";$ob2="";$ob3="";$ob4="";
$id=1111111;
$busqueda=$id;
try{    include("conexion2.php");                               
$base->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $base->exec("SET CHARACTER SET utf8");      
$sql="SELECT identificacion, saldo_credito, id_reg FROM 't_nextracto' WHERE identificacion=? ORDER BY id_reg ASC";
        //variable $resultado que almacena PDO statement
        $stmt=$base->prepare($sql);
        //ejecutar la funcion
        $result=$stmt->execute(array($busqueda));
        $rows=$stmt->fetchAll();
        //Acá no se como generar la rutina de codigo para tomar los datos como variables.
        print_r($rows);


        $stmt->closecursor();                           
        }catch(exception $e)
            {
                die('Error: ' . $e->GetMessage());                      
            }finally{
                $base=null;
        }

?>  
    
asked by Mauro CV08 02.09.2018 в 22:09
source

1 answer

0

Starting from what you already have done and assuming that you do not have any error when consulting the database;

It is necessary to pass a parameter to the method fetchAll to generate objects which will have the names of the columns of your query;

$rows=$stmt->fetchAll(PDO::FETCH_OBJ);

This way you can iterate the whole result of your query since you obtained the results as objects;

foreach($rows as $datos){
 echo $datos->identificacion;
 echo $datos->saldo_credito;
 echo $datos->id_reg;
}

This way you print on the screen what you get in your query, in the same way you can assign each result to a variable or as you wish.

I add the official documentation if you want to know more; link

In response to your comment in my answer, to save all of them in an arrangement (this is what you propose in your example) you can do it within the same cycle by adding a counter;

$contador = 0;
 foreach($rows as $datos){

     $var[$contador] $datos->saldo_credito;
     $contador++;

    }
print_r($var);
    
answered by 03.09.2018 / 01:23
source