How to assign the result of a select of a table to different variables

0

I make the query:

  SELECT cat_img, descripcion FROM 'CAT_PRODUCTO' WHERE id_gal BETWEEN 1 AND 7

Now I want to assign the result to different variables according to id_gal

example:

   $catimg_1: imagen extraída del registro id_gal 1

   $catimg_2: imagen extraída del registro id_gal 2

and so on all this with PHP PDO I hope to be clear with my doubts. I will appreciate any help

    
asked by Faratir 02.12.2017 в 14:20
source

1 answer

0

You can use extract($array); that expands an array to variables.

You arm the array with a while and then the extract, something like this:

<?php

$result = [];
$sql = "SELECT id_gal, cat_img, descripcion 
        FROM 'CAT_PRODUCTO' 
        WHERE id_gal BETWEEN 1 AND 7";
$sth = $conn->prepare($sql);
$sth->execute() or die($sth->errorInfo());
while ($row = $sth->fetch(PDO::FETCH_OBJ)) :
     $result["catimg_".$row->id_gal] = $row->cat_img;
endwhile;

var_dump($result);

extract($result);

echo "catimg_1: ".$catimg_1.PHP_EOL;
echo "catimg_2: ".$catimg_2.PHP_EOL;
echo "catimg_3: ".$catimg_3.PHP_EOL;

?>
    
answered by 06.08.2018 в 18:55