How to echo row in database with variable

I have a table in a database that has content and a php code.

Hello my name is <?php echo $var; ?>.

Whenever I echo the content from the table it does not execute the PHP code. It just displays the code.

Any advice on how I can get the PHP to execute.

To run a string as php code, use eval();

But why on earth would you be doing this?

I have previously tried using eval but I get the following error:

Parse error: syntax error, unexpected ‘<’ in hello.php(58) : eval()'d code on line 1

It will execute the entire string… “Hello my name is” is not valid php. You’d have to find Php tags, pull them out of the string and execute only that (after removing the php tags)

This works:


<?php
$var = 'omg';
$variable = 'Hello my name is <?php echo $var; ?>.';
preg_match_all('/<?php (.*?) \\?>/ms', $variable, $match);
eval(reset($match[1])); 

?>

Curious, what’s your use?

Are you trying to print the content from db.If yes,then this code can help you:


<?php
mysql_connect("hostname", "username", "password") && mysql_select_db("dbname") or die;
$details = mysql_query("select * from tablename");
while ($detail = mysql_fetch_assoc($details))
		{
		$name = $detail['name'];
		echo "$name";
		}
?>


Thanks!