0
        $SelectImg = "SELECT img FROM `fruits` ORDER BY RAND() LIMIT 2";
        $ImgResult = mysqli_query($db, $SelectImg); 
            foreach ($ImgResult as $value) {
                $value1 = $value ['img'];
                echo $value1;
            }

This codes displays $value1 as 'appleorange'. I want $value1 to be 'apple' and to have a $value2 with outputs 'orange'.

Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0
$SelectImg = "SELECT img FROM `fruits` ORDER BY RAND() LIMIT 2";
$ImgResult = mysqli_query($db, $SelectImg);

$results = [];

while ($value = mysqli_fetch_assoc($ImgResult)) {
    $results[] = $value['img'];
}

list($value1, $value2) = $results;

You are retrieving two results from the database. You write them into a one-dimensional array and the result is a two-element array. You assign these two array elements to two variables. VoilĂ .

Gander
  • 1,854
  • 1
  • 23
  • 30