SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist =X='s Avatar
    Join Date
    Jun 2001
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    executing php code located in mySQL

    hi
    I would think this is a newbie question, but in the official #php irc channel they couldn't answer so here it is:

    if you have some php code located in a cell in a database, eg. <?php print("hello"); ?> how can you execute that?

    [I]$content=mysql_query("SELECT text FROM pages WHERE name='$page'");
    if(!$content)header ("Location: 404.php");
    while($a = mysql_fetch_array($content))
    {
    $text=$a["text"];
    ???????
    }[I]

    so what do you have to place instead of the "??????" so it executes that code?

    and if you have a whole page in a cell, with javascript, normal text, html, php,... how do you make that to work properly?

  2. #2
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming your code is stored in the variable $mycode (im sure you can edit it to suit knowing this) the code below will do what you want...

    Code:
    eval($mycode);
    It really is that simple
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  3. #3
    SitePoint Evangelist =X='s Avatar
    Join Date
    Jun 2001
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thx, it helps a bit, but it's not really what I want

    if the text in that cell is: print("hello"); then eval works
    if the text in that cell is:hello, then it gives this error:
    Parse error: parse error in /...../test.php(170) : eval()'d code on line 1
    and the same with <?php print("hello"); ?>
    so it can only be used if there is only php code in it, but not on a normal page with html AND php...

  4. #4
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe...oh now i see the light

    That does complicate matters although i would think you could extract all the php segments using a regular expression, execute them using eval and print the rest out..

    I am no good at reg. exp. so i wont even try to write one for you at this time of night but im sure one of the masters (namely freddy) will be able to help!
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  5. #5
    SitePoint Evangelist =X='s Avatar
    Join Date
    Jun 2001
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah...I figured you would say something like that
    I'm not too good with regexps either


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •