Santosh Pillai
Santosh Pillai

Reputation: 1391

PHP variable name in mysql query

$resultSystem = mysql_query("SELECT * FROM templates where templateId = 1");
while($row = mysql_fetch_assoc($resultSystem))
{
$Part1 = $row["Part1"];
}

$name = "Peter";
echo $Part1;

In my database $Part1 results in $name. I would like the code to print the name "Peter", however it prints $name. How do I make PHP print the value of the variable?

Upvotes: 0

Views: 56

Answers (2)

senK
senK

Reputation: 2802

if $Part1 = 'name';

$name ="Peter";

Then

echo $$Part1 will print "Peter"

Upvotes: 2

Andrius Naruševičius
Andrius Naruševičius

Reputation: 8578

Use function eval() like

eval("echo $Part1");

However, be very careful so the users don't launch malicious code.

Upvotes: 0

Related Questions