Need help pulling PHP from SQL using EVAL-[SOLVED]
Moderator: General Moderators
Need help pulling PHP from SQL using EVAL-[SOLVED]
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...
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...
Last edited by ghank on Wed Apr 07, 2004 2:46 pm, edited 2 times in total.
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, how are you doing?
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 ?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Instead of putting the PHP code in the database, try using a place holder so that in the database you have something like:
Mac
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.:Hello {NAME}, how are you doing?
Code: Select all
$replaced_text = str_replace('{NAME}', $name_holding_variable, $text_from_db);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:
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?
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";
}
?>-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
{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.
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...
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";
}
?>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.
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.
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.
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.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK