Page 1 of 1
Including PHP script from MySQL
Posted: Wed Nov 27, 2002 10:32 am
by Gen-ik
Is it possible to read PHP code from a MySQL datatbase and include this in a php page... in a similar way you would use
include("file");?
I have tried several methods but none have worked so far.
Please help if ya can
Thanks.
Posted: Wed Nov 27, 2002 10:46 am
by BigE
Uh... no... you can't really execute PHP code in MySQL. However you can execute SQL in PHP from built in MySQL functions that are in PHP.
Posted: Wed Nov 27, 2002 11:41 am
by Gen-ik
Erm.. what I'm trying to do I can do like this...
Read a block of text from a MySQL database. Save this to a TXT file. Read this TXT file back into the PHP page using include().
But, this is takes time and slows down the script.
I'm just wondering if there is a more direct way of including text from a MySQL database into a PHP page. In this the 'text' just happens to be PHP script.
If I wanted to do this with normal text, and not PHP script, I could simply read from the database and echo() the text in the normal way.
Catch my drift?
Posted: Wed Nov 27, 2002 11:44 am
by BigE
If your wanting a way to query the database from PHP, check out the MySQL functions that are built into PHP. php.net/mysql If your talking about something else, explain a little better because I don't totally understand. Getting data into a dext file and then including that text file seems kinda recursive. Anyway, hope that helps.
Posted: Wed Nov 27, 2002 11:59 am
by volka
take a look at the
eval()-manual
e.g.
Code: Select all
<?php
$includeScript = <<<EOD
function tablecellHandler(\$content)
{
echo '<font color="#ff0000">', \$content, '</font>';
};
EOD;
eval($includeScript);
tablecellHandler('test');
?>
of course $includeScript could have been pulled from a db instead of a
heredoc, too

Posted: Wed Nov 27, 2002 12:07 pm
by Gen-ik
Thanks Volka.. you've hit the nail on the head
I should have thought of using eval() earlier. doh.
Posted: Wed Nov 27, 2002 12:11 pm
by BigE
Doh, misread, I'm doing that a lot today. Maybe I need sleep

I see what you mean now.
Posted: Wed Nov 27, 2002 5:39 pm
by Gen-ik
Ah, it's sort of working.
This is the script I'm using..
Code: Select all
<?php
$FINC=mysql_query(" SELECT * FROM fusionengine WHERE func='$INC' ");
while($thisFINC=mysql_fetch_array($FINC))
{
$FUSION=$thisFINCї1];
}
mysql_close($DB);
$FUSION=addslashes($FUSION); eval("\$FUSION = "$FUSION";"); $FINC = stripslashes($FUSION);
echo $FINC;
?>
The info read from the data base is this simple script (<?php and ?> is included)
<?php
echo("WOOHOOO!");
?>
What is happening is that the script is being embeded in the final displayed page.. so when I 'view source' the above script is sitting in the HTML page and hasn't be treated like PHP script.
This is the final result I'm getting..
<body>
<?php
echo("WOOHOO!");
?>
</body>
..from the HTML page.. which obviously I don't want.
Does anyone know how I would get the script to actually execute, and not just be dumped in with the HTML code?
Thanks.
Posted: Wed Nov 27, 2002 6:00 pm
by mydimension
i hate to say it but its doing exactly what you told it to do, your just misunderstanding the use of eval() (i think)
try changing the eval statement to this: eval($FUSION)
Posted: Wed Nov 27, 2002 6:15 pm
by Gen-ik
Right I've changed the eval() part so the code now looks like this...
Code: Select all
<?php
$FINC=mysql_query(" SELECT * FROM fusionengine WHERE func='$INC' ");
while($thisFINC=mysql_fetch_array($FINC))
{
$FUSION=$thisFINCї1];
}
mysql_close($DB);
$FUSION=addslashes($FUSION); eval($FUSION); $FINC = stripslashes($FUSION);
echo $FINC;
?>
But the final result is still being embeded in the HTML and not being executed.
I'm lost for a solution.
Posted: Wed Nov 27, 2002 6:39 pm
by mydimension
you say the eval()'d string has the php tags in it right? well try taking them out as they don't appear to be needed.
Posted: Wed Nov 27, 2002 6:53 pm
by volka
maybe another example helps clearing things up a little
try this one
Code: Select all
<html><body><?php
$codeToBeEvaled= <<<EOD
echo '<table border="1">';
for (\$x=0;\$x!=10; \$x++)
{
echo '<tr align="center">';
for (\$y=0; \$y!=10; \$y++)
echo '<td width="30">', \$x*\$y, '</td>';
echo '</tr>';
}
echo '</table>';
EOD;
echo '<fieldset><legend>content of $codeToBeEvaled</legend><pre>';
echo htmlentities($codeToBeEvaled);
echo '</pre></fieldset>';
echo '<fieldset><legend>result of $codeToBeEvaled evaluated</legend>';
eval ( $codeToBeEvaled );
echo '</fieldset>';
?></body></html>