Evaluate a string as PHP code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smartic
    New Member
    • May 2007
    • 150

    Evaluate a string as PHP code

    I need to Evaluate big chunk of a string as PHP code ,that code I'm getting it from the DB and publish it's data into page this page i want to deal with it as normal PHP page to include it into other pages ,how can i do that?
    This page (code.php) that get the code from DB:
    Code:
    <?php
    //that code for explanation not the real code
    mysql_connect('localhost','root','');
    mysql_select_db('testing');
    
    $result = mysql_query('SELECT * FROM PHP');
    while($row = mysql_fetch_assoc($result)){
        echo $row['CODE'].";\n";
    }
    ?>
    Here i need to include it as PHP page not as string:
    Code:
    <?php
    include('code.php');
    ?>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    try PHP: eval - Manual for the code evaluation.

    regards

    Comment

    • smartic
      New Member
      • May 2007
      • 150

      #3
      i need to Evaluate all the page code like:
      Code:
      <?php
      eval(include('code.php'));
      ?>
      put this give me an error is there is another way?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        eval() expects a string as input but include() doesn't have any return value, whatsoever. try to import the file as string, e.g. PHP: fopen - Manual and PHP: file_get_conten ts - Manual

        regards

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Try creating a function that loads and eval()s the code from the database. Then simply include a file with that function into your pages and call the function where it is needed.

          Like:
          [code=php]
          <?php
          # executeFunction .php
          function executeCode($co deID)
          {
          $sql = "SELECT code FROM codeTable WHERE codeID = $codeID";
          $result = mysql_query($sq l);

          if(mysql_num_ro ws($result) > 0) {
          $row = mysql_fetch_ass oc($result);
          eval($row['code']);
          }
          }
          ?>
          [/code]
          [code=php]
          # whatever.php
          include("execut eFunction.php") ;

          for($i = 0; $i < 10; $i++) {
          executeCode($i) ;
          }
          [/code]
          Get my thinking?

          Comment

          Working...