How can I get my php code to render correctly?

In my database, I have this text:<?php echo $id; ?>

Obviously, I need it to render in a way that looks like this:

When it's time to retrieve my text from the database, I use this syntax:

$header_query = "select * from email_header where id = '$blast_header'";
$header_result = mysqli_query($cxn, $header_query)
or die ("Couldn't execute query.");
$header_row = mysqli_fetch_assoc($header_result);
echo html_entity_decode($header_code);

...believing that "html_entity_decode($header_code) would identify the php delimiters and render it as code as opposed to text.

I'm wrong.

How do I fix that?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Please use var_export($header_row) and post the output in the code snippet here, thanks.  It may be necessary to use "view source" to get a true copy of the data, especially if there is HTML in your database.  This will enable us to see the data we are working with; then we can write the code.
William NettmannPHP Web DeveloperCommented:
Don't put "<?php echo $id; ?>" into your database, and you could then do
// Somewhere up here $id gets a value....
$id = 999;

$header_query = "select * from email_header where id = '$blast_header'";
$header_result = mysqli_query($cxn, $header_query)
or die ("Couldn't execute query.");
$header_row = mysqli_fetch_assoc($header_result);
echo "$header_row$id";
// or
echo $header_row . $id;
// whichever you prefer.

Open in new window

Ray PaseurCommented:
It's probably wise to test the code before using it.  And we can do that as soon as the Author posts the test data from the $header_row variable.

PHP extract() needs to go away.  Anything that proliferates variables or injects names into the symbol table is an anti-practice.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Bruce GustPHP DeveloperAuthor Commented:
Gentlemen! Sorry for not getting back with you sooner. I've got a couple of plates spinning, some of which needed attention more so than this quandary that you've been kind enough to weigh in on.

You can see an example of what I'm dealing with by heading out to That's the "rendering" of the data that I've inputted.

So, in my admin suite, when I'm getting ready to enter the code that constitutes the header for a particular email, I'll put in the text field:

<A HREF="<?php echo $id; ?>" target="_blank">View this message in a browser...</a>  <BR> <BR>

Open in new window

When it's time to insert that info into my database, I'm using this:

$header_code = htmlentities(trim($_POST['header_code']));

$insert = "insert into email_header (header_name, header_code)
values ('$header_name', '$header_code')";
$insertexe = mysqli_query($cxn, $insert);
if(!$insertexe) {
$error = mysqli_errno($cxn).': '.mysqli_error($cxn);

Open in new window

No errors, everything seems fine, but then when you look at the way things are actually being rendered, you get this (line 19 of the source code):

<A HREF="<?php echo $id;?>

It looks as thought the delimters etc are being processed correctly, but once you hit "echo $id;," it's no longer being processed as php and therein lies my dilemma.

What do you think?
William NettmannPHP Web DeveloperCommented:

Bottom line: you can't just include "<?php echo $id; ?>" into your text field, simply because it is a text field.

html_entity_decode decodes HTML entities, it does not execute PHP code. It would convert "&lt;" to "<" for example.

Put this into your text field:
<A HREF='$id' target='_blank'>View this message in a browser...</a>  <BR> <BR>

Open in new window

Then do:
$string = html_entity_decode($header_code);
echo evaluate($string);

Open in new window

Not very elegant, but it could work, everything else being equal.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bruce GustPHP DeveloperAuthor Commented:
That will work!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.