Hello all.

I've been banging my head against the wall since yesterday trying to solve this problem and, needless to say, it hasn't worked. It just cracked my skull and made a mess on the wall...anyhow....

I have written a script to search through my mySQL database for user-entered keywords. It works perfectly as a stand-alone script, but...

I'm using PHPLIB's templates.inc file for a php/mysql driven site. It mostly works like a charm. When you put a search word into the form an push the button, it is supposed to search the database for the word and return a set of results. The code for my search function lives in the database along with all the other site content.

The problem I am having is with the eval() function. In order to properly execute my php-based search function, I have to include it in the template from the database and, once the template has all it's holes filled, eval() it again to run the search feature.

The error I am getting is as follows:

Parse error: parse error in /home/cust2/usr2049/html/web.php3(237) : eval()'d code on line 366

Below is an excerpt from the search source code. i will clearly mark line 366 (look for '!!!-> LINE 366 in eval()'d code!!!! ').

/ This is the total number of words in the array $search_words/
$search_num = count($search_words);

/ Here we define the $table array. All the tables to be searched should be in here. /
$table = array("main","ilinks","about","fbs","awards","finder","news");
$table_num = count($table);

/
This for loop builds the query to be run on the tables,
then executes the query on each table and returns the results.
The loop code is from here to the end of this file.
/

echo "<h4>Your Search Returned The Following Results for:&nbsp;$search</h4>";

/ This line beginning with 'for' basically says 'Do this for each table' /
for ($i = 0; $i < $table_num; ++$i) {

/* This $statement variable is sufficient if there's only one search word */
$statement = "SELECT tbl, page, title, description FROM $table[$i] WHERE keywords LIKE '%$search_words[0]%' ";

/* This chunk of code adds to $statement if there is more than one search word.
   for each occurrence of an additional search word the phrase
   'OR keywords LIKE additional-word' is added to $statement */
if ($search_num > 1){

    for ($idx = 1; $idx < $search_num; ++$idx){

        $statement .= " OR keywords LIKE ";
        $statement .= "'%$search_words[$idx]%' ";
        }
        }

/ This is the MySQL info. "server", "username", "password" /
$db = mysql_pconnect("server", "username", "password");
mysql_select_db("DATABASE", $db);

/ Here is where the query is actually submitted to MySQL for a result.
Notice: it doesn't query the whole DB at once, rather it queries whichever table the for loop is on
/
$result = @($statement, $db);

$num_of_rows = @mysql_num_rows($result);

/ This little widget assembles each result and spits it out to the browser.
Each matching row in the table is spit into an array called $row, similar to a CSV file.
$row[0] is the 'table' field, $row[1] is the 'page' field, $row[2] is the 'title' field, etc.
/
if($num_of_rows > 0) {
while ($row = mysql_fetch_row($result)) {

!!!-> LINE 366 in eval()'d code!!!! echo "<p><a href=\"../../web/$row[0]/$row[1].html\">
$row[2]</a><br>$row[3]<br><font color='#57A275'>www.mbinet.org/web/$row[0]/$row[1].html</font></p>
";
}
}
}

?>

Well, That's about it...the above script works perfectly as its own .php3 file, but bombs when i try to eval() it.

Please, Please, Help me! I'll be so sad that I won't even want Ice Cream if this foobar don't get fixed!

-dolphinsnot

    Now, why aren't you using phplib as it is ment to be used? You're not using the sql class for sure....

    Now a question: Do I understand correctly that this code is stored in the database as a php page so a request works as:

    broweser>server>php>
    db -- get the page
    php> eval the page
    db -- get the info needed for the page.

    Will you post the code that does the eval too?

    Tom

      thank ya. you correctly understand how the widget requests work.

      for my current project (and my first with php), i'm converting a 1,000 page static .html site that lives in a directory structure over to 1 php script (web.php3), 2 template (one for ie/win, one for others), and 1 MySQL db (subdivided into tables based on content; about,links,main,magazine,members,etc.).

      I currently only need the template feature of phplib. I plan to incorporate the other elements once I complete this basic phase. The template has empty slots for CSS sizes, meta-tags, and page content, along with a few slots to turn page elements 'on' or 'off' with comment tags.

      The page works with search-engine and novice-computer-user friendly URLs. In other words, instead of www.website.org/web.php3?table=about&page=index, the page reference would be www.website.org/web/about/index.html.

      My search function is called like any other page. There's a little search window in my template that appears on every page, and the submit button has an action of www.website.org/web/search/results.html.

      below is the template section of my web.php3 script. The part that does the eval() is at the very bottom (named $magicfairy). I left all of my comments in just in case they might be helpful to anyone...

      /
      $vars de-cloaks the necessary page assembly variables hidden in the URL.
      It basically says $vars=whatever is in the URL after 'web', from the 2nd to 6th from last character.
      This removes the '/' and '.html' from whatever is after 'web'
      www.website.org/web/main/index.html is stripped down to 'main/index'
      /
      $vars = substr($PATH_INFO, 1, -5);

      / This splits the de-cloaked variables into 2 parts of an array
      /$slash = "/";
      $var_array = split($slash, $vars);

      / This assigns $table and $page the proper values /
      $table = $var_array[0];
      $page= $var_array[1];

      / Database connection stuff. "server", "username", "password" /
      $db = mysql_connect("foo", "bar", "foobar");
      / Database name - note: Database name and Username are the same for us /
      mysql_select_db("foo", $db);

      / This selects the proper page info from the MySQL Database /
      $result = mysql_query("SELECT * FROM $table where page=\"$page\"", $db);

      / This includes the code for the template engine /
      include "/templates/template.inc";

      / Here we assign each of the necessary variables their values from MySQL
      some variables, such as the CSS vars and the $template var are already defined
      in the CSS GENERATION section above
      /
      $title = mysql_result($result,$page,"title");
      $expires = mysql_result($result,$page,"expires");
      $robots = mysql_result($result,$page,"robots");
      $description = mysql_result($result,$page,"description");
      $keywords = mysql_result($result,$page,"keywords");
      $ad_zone = mysql_result($result,$page,"ad_zone");
      $location = "- MBI > $table > $page -";
      $content = mysql_result($result,$page,"content");

      / This line creates a new temporary file for use with the template engine /
      $t = new Template("/home/cust2/usr2049/templates/");

      / Choose template file. /
      $t->set_file("website",$template);

      / Determine if banner ad cell at top of template is on or off /
      if ($ad_zone == "z") {
      $t->set_var("onoroff1","<!--");
      $t->set_var("onoroff2","-->");
      } else {
      $t->set_var("onoroff1"," ");
      $t->set_var("onoroff2"," ");
      }

      / This lets the template engine know how our variable names are referred to in the template file.
      For examplpe, we let the template engine know that when it finds the phrase "page_title" in curly
      brackets it is to replace the phrase with the value of $title
      /
      $t->set_var("page_title",$title);
      $t->set_var("page_expires",$expires);
      $t->set_var("page_robots",$robots);
      $t->set_var("page_description",$description);
      $t->set_var("page_keywords",$keywords);
      $t->set_var("ad_zone",$ad_zone);
      $t->set_var("your_location",$location);
      $t->set_var("page_content",$content);
      $t->set_var("font_size",$font_size);
      $t->set_var("font_smaller",$font_smaller);
      $t->set_var("font_smallest",$font_smallest);
      $t->set_var("site_fonts",$site_fonts);

      / This is where the final parsing takes place. By default, only the line:
      '$t->parse("weboutput","website");' would be used by the engine. I had to
      create the $magicfairy in order to be able to place PHP code like the search function
      directly in the database. This way, the file gets parsed, Content gets inserted, then
      The MagicFairy runs back through the file and executes any PHP code it finds
      /
      $t->parse("weboutput","website");
      $magicfairy = $t->parse("weboutput","website");
      eval ("?>".$magicfairy);

      ?>

      The above code works great for all pages that don't contain php code pulled from the database.

      Thank you for help!

      -dolphinsnot

        K, first, you really should switch to using the sql class provided by phplib.

        Next, these are the bugger lines:
        $t->parse("weboutput","website");
        $magicfairy = $t->parse("weboutput","website");
        eval ("?>".$magicfairy);

        The first and second lines do the same thing with no benefit. These three lines should be simplified into

        eval('?>'.$t->parse('weboutput', 'website'));

        However, to debug this, do
        $t->pparse('weboutput', 'website');
        die();

        That way you can see the exact php that it is trying to eval and you'll have a better chance of debugging it.

        I suspect it's related to bad php code in the 'template' you're pulling in.

        Next, $t->set_var() can take an array to simplify code

        $t->set_var(array(
        'key1' => 'val1',
        'key2' => 'val2'
        ));

          Write a Reply...