Jump to content

PHP in MySQL


arneman

Recommended Posts

I want to make a tutorial website, but making the PHP tutorial I ran into a little problem. I usually save my tutorials in MySQL in HTML. This works fine, the HTML is shown as text with the correct layout.

 

The PHP only works partially. I want to do the following:

 

<p>test</p>

<?php

highlight_string('echo "test";');

?>

 

On my screen i see, 'echo "test"' but without the highlights. Any suggestions on how to change this?

Link to comment
https://forums.phpfreaks.com/topic/164444-php-in-mysql/
Share on other sites

well, if your trying to output the code (w/o highlight), you need to use htmlentities() on the result. If you want the php being returned in the result to process as php, your need to use eval().

WARNING: eval() will allow several attacks on your site if you allow user submitted data to pass through it. For example, some one inputs

<?php print_r($_SESSION); ?>

or other sensitive data is echoed out to the end user.

Link to comment
https://forums.phpfreaks.com/topic/164444-php-in-mysql/#findComment-867448
Share on other sites

If you put this php code -

 

<?php
highlight_string('echo "test";');
?>

 

into a .php file on a server with php installed and browse to the file, you get this resulting HTML content -

 

<code><span style="color: #000000">
echo "test";</span>
</code>

 

eval() has absolutely nothing to do with this, don't attempt to use it.

Link to comment
https://forums.phpfreaks.com/topic/164444-php-in-mysql/#findComment-867495
Share on other sites

I'm going to guess that you actually wanted to do the following -

$some_code = '<p>test</p>
<?php
echo "test";
?>'; // where $some_code is what you retrieved from the database?
highlight_string($some_code);

 

Which produces this HTML output -

 

<code><span style="color: #000000">
<p>test</p>
<br /><span style="color: #0000BB"><?php
<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"test"</span><span style="color: #007700">;
<br /></span><span style="color: #0000BB">?></span>
</span>
</code>

Link to comment
https://forums.phpfreaks.com/topic/164444-php-in-mysql/#findComment-867519
Share on other sites

Based on that, I finally get what you are trying to do -

 

<p>test</p> is HTML content on the page to serve as a title for the code that follows it.

 

You should be using some type of a template with variables $title, $code or place holders {TITLE}, {CODE} that you replace with the actual values when you display the section of the page. And yes, the title and the code should be stored in separate columns in the table row.

 

For the example you have been using -

 

$title = 'test';
$code = highlight_string('echo "test";',TRUE);

// either directly echo the template or have it exist in a variable that is echoed later

echo "<p>$title</p>
$code";

Link to comment
https://forums.phpfreaks.com/topic/164444-php-in-mysql/#findComment-867576
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.