qadenza
qadenza

Reputation: 9293

declare and give value to variables stored in a table

I have a table vars containing variables only. There are three columns:
id - id
name - variable name
val - variable value

I need to declare that variables and give them corresponding values, something like:

$stmt = $db->query("SELECT * FROM vars order by id asc");
while($row = $stmt->fetch()){
    '$' . $row['name'] = "'" . $row['val'] . "'";
}

echo $somevariable...

Any help?

Upvotes: 0

Views: 37

Answers (3)

Ahmed Shefeer
Ahmed Shefeer

Reputation: 395

$stmt = $db->query("SELECT * FROM vars order by id asc");
while($row = $stmt->fetch()){
    ${$row['name']} = $row['val'];
}

Try this However you need to make sure that $row['name'] is compliant with PHP variable naming requirements

Upvotes: 1

Sudhanshu Jain
Sudhanshu Jain

Reputation: 534

try this

${$row['name']} = $row['val'];

Upvotes: 1

invisal
invisal

Reputation: 11171

I think you want to do something like this

$$row['name'] = $row['val'];

Upvotes: -1

Related Questions