Nita Nita - 3 months ago 12
MySQL Question

Display values from php mysql array separately

What im trying to do is to save vales of "option" from all records in array to be able to echo them out later. I don't know how to display them seperatly. Feel stuck. Any sugestions?

My code:

$result = $mysqli->query("SELECT option FROM `myoptions` WHERE id IN (1, 7, 18, 24);");
$row = array();
while($row = $result->fetch_array()) {
echo $row[] = $row["option"];
}

echo $option1 = $row[0];
echo $option2 = $row[1];
echo $option3 = $row[2];
echo $option4 = $row[3];

Answer

YOu should store the value in an array

$cnt = 0;
while($row = $result->fetch_array())  { 

  $for_future_use[$cnt] = $row["option"]; 
  $cnt++; 
}

and then use later

foreach($for_future_use as $key=>$value) {
   echo $value;
}
Comments