0

I have a MySQL table named global_variables with three columns id, name, value. I'd like to loop through all rows and make the name column the name of the variable and the value column its value. Sample data row 1

`id`='1'
`name`='age'
`value`='35'

row 2

`id`='2'
`name`='gender'
`value`='Male'

Here is my code

$result = $db->query("SELECT `name`, `value` FROM `global_variables`");
while($row = $result->fetch_array()){
    $$row['variable_name'] = $row['variable_value'];
}
echo $age; //This should display 35
echo $gender; //This should display Male

The problem is in the while loop at $$row['variable_name']. How do implement naming a variable using the name column and assigning its value as the value column?

Brian
  • 3,850
  • 3
  • 21
  • 37
Slimfresh
  • 13
  • 4

0 Answers0