Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Process PHP from a MySQL array

  1. #1
    tnl2k7 is offline Banned tnl2k7 is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    #offtopic ;)
    Posts
    3,131

    Process PHP from a MySQL array

    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 by tnl2k7; 05-05-2008 at 01:22 AM. Reason: URL changed

  2. #2
    sonicsshadow is offline x10 Sophmore sonicsshadow is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    198

    Re: Process PHP from a MySQL array

    Why don't you just store the values into the table rather than the actual code?
    Did I misunderstand you?

  3. #3
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: Process PHP from a MySQL array

    "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.
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  4. #4
    tnl2k7 is offline Banned tnl2k7 is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    #offtopic ;)
    Posts
    3,131

    Re: Process PHP from a MySQL array

    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 by tnl2k7; 05-05-2008 at 12:40 AM.

  5. #5
    LHVWB's Avatar
    LHVWB is offline Lord Of The Keys LHVWB is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Australia
    Posts
    1,308

    Re: Process PHP from a MySQL array

    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 by LHVWB; 05-05-2008 at 12:58 AM.

  6. #6
    tnl2k7 is offline Banned tnl2k7 is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    #offtopic ;)
    Posts
    3,131

    Re: Process PHP from a MySQL array

    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 by tnl2k7; 05-05-2008 at 03:11 AM.

  7. #7
    LHVWB's Avatar
    LHVWB is offline Lord Of The Keys LHVWB is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Australia
    Posts
    1,308

    Re: Process PHP from a MySQL array

    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 Code:
    <?php

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

    eval(
    $string);

    ?>
    The "Testing" text can be completely replaced by any html code.

  8. #8
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: Process PHP from a MySQL array

    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.
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  9. #9
    tnl2k7 is offline Banned tnl2k7 is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    #offtopic ;)
    Posts
    3,131

    Re: Process PHP from a MySQL array

    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 by tnl2k7; 05-05-2008 at 04:09 AM.

  10. #10
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: Process PHP from a MySQL array

    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?
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

Closed Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. php and mysql help:i have an issue intergrating php and msql
    By mafisco27 in forum Programming Help
    Replies: 5
    Last Post: 03-18-2008, 12:35 AM
  2. Replies: 0
    Last Post: 03-11-2008, 06:01 PM
  3. Replies: 3
    Last Post: 03-10-2008, 12:22 PM
  4. Sigo con problemas con phpbb2
    By reciecho in forum Soporte
    Replies: 7
    Last Post: 10-20-2007, 06:28 PM
  5. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers