Fetching PHP code from table field in database


I have a table notifications where I have a field notification which has value:

<?php echo '.$user['name'][0].' ?>, you are <?php echo popularity(); ?>% popular today.

The output is: , you are % popular today.

It is ignoring the PHP here. I don’t understand why.

Expected output is Shubham, you are 35% popular today.

Under network in inspect element the response tab with raw data shows this:

<div class="notiText">
  <?php echo $user['name'][0]; ?> you are <?php echo popularity(); ?>% popular today.

What’s the issue here? Why does the script does not load PHP tags from the database?


I have tried using eval() from here Reading php code from database. However, I have to do that into the database and not in code. The new output has this eval(), you are eval()% popular today.. Still not reading.

Note: I cannot use eval() into the coding. This has to be dynamic and from database. Therefore, anyone who is thinking of suggesting eval() I need an alternative or a workaround to the situation with eval() and database.


Here is the sample code:


$stmt = $pdo->prepare("SELECT notification FROM notifications");
$stmt-> execute();

$html = "<div class='notifications'>";
while($f = $stmt->fetch()){
  $html .= '<a href="'.$link.'">'.$f['notification'].'</a>';
$html .= "</div>";
echo $html;


$(".notificationsBell").on("click", function(e){
    type: "POST",
    url: "processes/notifications.php",
    data: "all",
    success: function(data){


<div class="loadNotifications"></div>


Okay so the following code is giving me the proper output.

$str = ''.$user['name'][0].', you are '.popularity().'% popular today.';

eval("\$str = \"$str\";");
echo $str;

Output: Shubham Jha, you are 100% popular today.

Now, how do I insert that string (the value of $str) into the database? In the table field what should be the format so that it gets recognized and echoed correctly. I tried this but doesn’t work.

'.$user['name'][0].', you are '.popularity().'% popular today. 

Returns this error:

Parse error: syntax error, unexpected string content "", expecting "-" or identifier or variable or number in E:\xampp\htdocs\newchat\processes\notifications.php(65) : eval()'d code on line 1


Try this:

while($f = $stmt->fetch()){
  $name = $user['name'][0];
  $popularity = popularity();

  $notiText = $f['notification'];
  eval("\$notiText = \"$notiText\";");

  $html .= '<a href="'.$link.'">'.$notiText.'</a>';

In the database use these simple variable names and avoid using quotes.

$name, you are $popularity% popular today.

Answered By – RedStar Entertainment

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published