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
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
Share on other sites

You are trying to display php source code as part of a tutorial, using the highlight_string() function to give it some color, correct?

 

So, are you doing this in a .php file, on a web server with php installed and functional?

Link to comment
Share on other sites

You are correct!

 

In my PHP file, I query a MySQL database containing the complete tutorial written in HTML.

 

As I said before, the HTML executes, but the PHP doesn't, or at least not correctly.

 

Edit: Yes I am on a webserver with everything installed.

Link to comment
Share on other sites

Well I tried the Eval() but the PHP manual on that part isn't completely clear to me. So I put the following in my DB:

 

<?php

$code = 'highlight_string(\'echo "test";\')';

eval($code);

echo $code;

?>

 

Unfortunately it doesn't work :P

Link to comment
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.