Process PHP from a MySQL array

Status
Not open for further replies.

tnl2k7

Banned
Messages
3,131
Reaction score
0
Points
0
Right,

I'm pretty new to PHP AND MySQL, so if you reply please could you provide a code snippet preferably with comments telling me what each line does?

Basically, I'm trying to use a content management system I wrote a while back in a way I never wrote it for. I'm gonna have to re-write it which is fine, but I don't know how to make it work.

I want to be able to process PHP code inside a variable that was retrieved from a MySQL request. For example, a user to goes to index.php?id=example. I then run a MySQL query:
Code:
 $id = $_GET['id'];
mysql_connect("localhost", "hidden", "hidden");
 mysql_select_db("hidden");
 $result = mysql_query("SELECT * FROM pages WHERE name='$id'") or die(mysql_error());
  $row = mysql_fetch_array( $result ) 
 $content = $row['content']
to get this content from the database. When I send it to the browser using:
Code:
echo "$content";
it outputs a load of PHP code rather than processing it and outputting that. How can I make PHP process the PHP code in the MySQL row rather than just sending it to the browser?

You can see the example I'm talking about with the PHP output here.

All help is appreciated and to the person who comes up with the best solution, I'll pay you 300 credits; I need help badly.

Thanks,
-Luke.
 
Last edited:

sonicsshadow

Member
Messages
200
Reaction score
0
Points
16
Why don't you just store the values into the table rather than the actual code?
Did I misunderstand you?
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
"If eval() is the answer, you're almost certainly asking the wrong question" - Rasmus Lerdorf (Creator of PHP)

With that said, the answer to your question is eval() :p

http://www.php.net/manual/en/function.eval.php

eval() can be used to evaluate a string of php code, however it's very inefficient. I have never come across a situation where the code could not be written in a (better) way that doesn't involve eval(). If you're just doing this for the purpose of testing, then perhaps it's ok to use it. Although some would argue that it's better to forget it even exists.

So, I would say that if you can't come up with a better way to execute the code without eval(), then just explain what it is that the code is supposed to do and I'm sure that I or someone else will be able to help you.
 

tnl2k7

Banned
Messages
3,131
Reaction score
0
Points
0
Well basically instead of having to type out code for the header, footer, links and everything I put all that into index.php and made it so whenever you go to index.php?id=example it looks in a MySQL table callled pages for a row named example, and then retrieves the content field and sets as a vatiable, $content.

The problem is, there's PHP code in the pages stored within the database and I'd like to process instead of having to create static pages for everything - otherwise there's no point; heck has this plan has gone wrong or what?

Any ideas?
-Luke.
 
Last edited:

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
Use the eval() function. It will run the php code that you pass it. I have seen this used in other examples where people are storing php code in their database which they want to run, an example of this is the CustomAction Mod for SMF. It stores the information in the database and gets it when index.php?action=(entry) is called.
 
Last edited:

tnl2k7

Banned
Messages
3,131
Reaction score
0
Points
0
It's just a little worrying how much longer eval() takes to execute though, I'm honestly thinking about forgetting it all and going back to the old include() based one.

Computers really annoy me sometimes :rant2:

I'll try using eval and see if it works but I don't think it's worth the trouble really. I may as well go back to the old method.

Edit:

Nope, eval doesn't work anyway because I've got HTML code in there as well. Flipping heck this is annoying. How can PHP be so completely and utterly illogical sometimes? I'd expect there to be a way to force it to parse the contents of the MySQL request but clearly I'm the only person who'd find this useful.

Anyway unless someone has a solution I think I'll give up and go back to a PHP include, this is just inconvenient and dumb.

Edit:

Right, I even tried using an include to a page which gets the mysql entry but still no luck. Does anyone have a better way to do this which doesn't involve the foul and ugly eval function? Using it would mean I have to re-write everything and to be honest I can't see the point. I hate PHP.
 
Last edited:

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
Have a closer look at the documentation for the eval() function, it says.
To mix HTML output and PHP code you can use a closing PHP tag to leave PHP mode.

So you could do something like this.
PHP:
<?php

$string = "?> Testing <?php  phpinfo();";

eval($string);

?>

The "Testing" text can be completely replaced by any html code. :biggrin:
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Executing code from a string already goes against good programming practice. But to store what should be its own file in a string and execute it is just insane. I strongly recommend against this.

What exactly is it that the code which would be eval()'d is supposed to do? If you meant to apply this generally across different scripts, then please give an example or two. I am very certain that you can achieve the same effect in a more efficient/logical way by storing the appropriate values in the db and writing the appropriate code in a file.
 

tnl2k7

Banned
Messages
3,131
Reaction score
0
Points
0
I'm only doing it to retrieve the last 5 items added to a MySQL table, then the the 5 highest rated... the rest is all HTML.

The code I'm using for the latest entries is:
Code:
  $result = mysql_query('SELECT * FROM games ORDER BY id DESC LIMIT 5') or die(mysql_error());
  while($row = mysql_fetch_array( $result )) {
   $link = str_replace("-"," ",$row['name']);
   echo "<li><a href=\"play.php?id=";
   echo $row['name'];
   echo "\">";
   echo $link;
   echo "</a></li>";
and for the highest rated entries I'm using:
Code:
  include("shared/script/mysql-connect.php");
  $result = mysql_query('SELECT * FROM games ORDER BY rating + 0 DESC LIMIT 5') or die(mysql_error());
  while($row = mysql_fetch_array( $result )) {
   $link = str_replace("-"," ",$row['name']);
   echo "<li><a href=\"play.php?id=";
   echo $row['name'];
   echo "\">";
   echo $link;
   echo "</a> - Rating: ";
   echo $row['rating'];
   echo "</li>";
  }
I think I'll give up and just have all static files, this is really starting to irritate me bigtime now. I was doing this for ease of editing later but it seems to have gone horribly wrong lol.

Thanks guys, I know I'm a noob and probably the world's most annoying person but this is driving me insane,
-Luke.
 
Last edited:

woiwky

New Member
Messages
390
Reaction score
0
Points
0
I'm a little confused, sorry. Is that the code that's stored in the db or is that in the file? If it's stored in the db, what's wrong with putting it in the file? And if it's stored in the file, where's the code that comes from the db?
 

tnl2k7

Banned
Messages
3,131
Reaction score
0
Points
0
Hi guys,

Sorry, I didn't make myself clear. The code above is in the database. I don't want to put it into a file because I've written an admin panel for the whole site (unbelievable, I know) and I'd like to be able to edit pages. It makes it easier to maintain than having to FTP files over to the server all the time. The entire thing is fine, but PHP won't parse the PHP code within the array retreived from the database, if that makes sense?

I can send HTML fine, but the PHP just gets sent to the browser rather than being parsed first. As all the site's pages bar one need PHP code, this is a slight problem. This is getting really complicated.....

-Luke.
 
Last edited:

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
It definetly sounds like you are going the wrong way about this. You should either use includes or put the actual php code straight into your current php file.

If you really want to keep the admin cp then you can use the php file edit functions, such as file_get_contents(), or file(), along with some saving of the files with the file_put_contents() function.

This allows you to keep your admin cp and also edit the files easily.
 

tnl2k7

Banned
Messages
3,131
Reaction score
0
Points
0
Hi,

Problem solved, I've fixed this another way. 300 credits each have been sent to verbsite and woiwky.

Thanks guys,
-Luke.

* Closed *
 
Status
Not open for further replies.
Top