Page 1 of 1

Need help pulling PHP from SQL using EVAL-[SOLVED]

Posted: Wed Apr 07, 2004 1:46 am
by ghank
Been working on this allllll day. Ok, I am using a PHP file to query a MYSQL database. That database has some text with a variable in the middle that is defined in the PHP file. ie. - PHP has a keyword I want to use and it queries the DB and gets text which contains php code that should insert the keyword defined in the PHP file.

I understand the EVAL function will do this. But I can't resolve it. I found several references to this but they don't seem to work. I am pretty sure the PHP in my DB is correct.

Would it be more effecient to use the EVAL function (assuming I can get it to work) or would it be better to insert the keyword from the php file into the DB and have MySql insert it into the text I retrieve? Any help would be much appreciated. Keep in mind I started using PHP 4 days ago and Mysql about 10 days ago...

Posted: Wed Apr 07, 2004 11:12 am
by Steveo31
I don't see what you mean with the eval() and the task you need to do. You just need to do a SELECT statement and find that keyword with PHP using strpos or somethin. Could you elaborate more?

:)

Posted: Wed Apr 07, 2004 11:27 am
by ghank
Thanks for the help. Let me try to explain:

My main php file sets the variable. I'll probably have several thousand of these files each with different variables. And it will query the DB and pull a few paragraphs of text which will be the body of my document. I want that variable to be inserted here and there inside the DB text. But when it pulls the data it's not parsed and you can view the php code in the source. So I've read that eval can do that.

php sets the variable to "John". (I tried using a session variable)

I want it to query the DB and spit out:

Hello John, how are you doing?

Instead it returns:

Hello

Code: Select all

<?php code is in the middle ?>
, how are you doing?

Posted: Wed Apr 07, 2004 11:38 am
by twigletmac
Instead of putting the PHP code in the database, try using a place holder so that in the database you have something like:
Hello {NAME}, how are you doing?
Then when you retrieve the information from the database use [php_man]str_replace[/php_man]() to insert the variable in place of {NAME}, e.g.:

Code: Select all

$replaced_text = str_replace('{NAME}', $name_holding_variable, $text_from_db);
Mac

Posted: Wed Apr 07, 2004 12:00 pm
by ghank
Thanks for the tip! That sounds like it is what I need but I can't get it to work. Here is my code right now:

Code: Select all

<?php

$keyword1="Keyword";

$replaced_text = str_replace('{NAME}', $keyword1, $row[1]);

//connect to server with username and password
$connection = mysql_connect ("host","user", "pswd") or die ("Cannot make the connection");
//connect to database
$db = mysql_select_db ("db name",$connection) or die ("Cannot connect to database");
//our SQL query
$sql_query = "SELECT * FROM table ORDER BY RAND() LIMIT 1";
//store the SQL query in the result variable
$result = mysql_query($sql_query);
if(mysql_num_rows($result))
{
//output as long as there are still available fields
while($row = mysql_fetch_row($result))
{
echo ("$replaced_text");
}
}
//if no fields exist
else
{
echo "no values in the database";
}
?>
This returns "$replaced_text" instead of the mixed text. I changed the DB to your example above "Hello {NAME}, how are you doing?". Any ideas?

Posted: Wed Apr 07, 2004 1:28 pm
by ghank
Bump!

If anybody can help I'd be happy to tip you $25 via Paypal or donate the money to this forum. I am going on 12 hours on this problem and I am going nutz!

Posted: Wed Apr 07, 2004 2:05 pm
by magicrobotmonkey
a)You don't need $replaced_texzt in " "'s
b)you know that you are echo-ing just the text, nothing from the DB?

Posted: Wed Apr 07, 2004 2:09 pm
by ghank
I'm not sure what you mean. But yes, I know I am just echoing the text and there in lies the problem. Keep in mind I've been working with PHP for about 4 days so I am very new to this. What should I have to output the variable?

Posted: Wed Apr 07, 2004 2:18 pm
by magicrobotmonkey
what do you have in the DB that you are trying to get out and what are you trying to do to it?

Posted: Wed Apr 07, 2004 2:24 pm
by Steveo31
{NAME}, if anything, has to be in double quotes. Single quotes don't parse variables and such. In addition, the str_replace line has the variable $row in there, and $row is defined later on.

Code: Select all

<?php 
//connect to server with username and password 
$connection = mysql_connect ("host","user", "pswd") or die ("Cannot make the connection"); 
//connect to database 
$db = mysql_select_db ("db name",$connection) or die ("Cannot connect to database"); 
//our SQL query 
$sql_query = "SELECT * FROM table ORDER BY RAND() LIMIT 1"; 
//store the SQL query in the result variable 
$result = mysql_query($sql_query); 
if(mysql_num_rows($result)) 
{ 
//output as long as there are still available fields 
while($row = mysql_fetch_row($result)) 
{ 
$keyword1="Keyword"; 
$replaced_text = str_replace("{NAME}", $keyword1, $row[1]); 
echo ("$replaced_text"); 
} 
} 
//if no fields exist 
else 
{ 
echo "no values in the database"; 
} 
?>
Also, where is the value of {NAME} being set? I would guess it would have to be like $row['name'] as an associative array, and not row numbers...

Posted: Wed Apr 07, 2004 2:30 pm
by ghank
Here is some background info. I am making a website with relevant randomized content so the search engines will think each page is unique. The DB will contain 3 tables each with about 20 unique paragraphs. First table has the first paragraph in the document, 2nd table has 2nd paragraph, etc. And I want my keyword inserted throughout the paragraphs to aid with search engine optimization. So my php file sets a variable (my keyword) and the pulls the paragraphs from the DB and HOPEFULLY inserts the keyword through the paragraphs. I was making individual html files that did this but it took forever so I am hoping this will be a huge shortcut.

Right now I am pretty clueless as to how to do this as many cups of coffee and lack of sleep has turned my brain to mush.

Posted: Wed Apr 07, 2004 2:42 pm
by ghank
Steveo31!!!

You Rule!!! It works! I can finally sleep... Hey, if you email me your email address I'll send you $25 via Paypal! greg@mrski.com

Thank You! Thank You! Thank You! Thank You! And thanks to everybody else trying to help! Can anybody recommend a good PHP basic book? I need to study up on this.

Posted: Thu Apr 08, 2004 9:01 am
by twigletmac
Steveo31 wrote:{NAME}, if anything, has to be in double quotes. Single quotes don't parse variables and such.
Just want to correct one misconception - {NAME} is not a variable it is a string, therefore it is fine in either single or double quotes.

Mac