Including PHP script from MySQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Including PHP script from MySQL

Post 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.
User avatar
BigE
Site Admin
Posts: 139
Joined: Fri Apr 19, 2002 9:49 am
Location: Missouri, USA
Contact:

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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?
User avatar
BigE
Site Admin
Posts: 139
Joined: Fri Apr 19, 2002 9:49 am
Location: Missouri, USA
Contact:

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Thanks Volka.. you've hit the nail on the head :)

I should have thought of using eval() earlier. doh.
User avatar
BigE
Site Admin
Posts: 139
Joined: Fri Apr 19, 2002 9:49 am
Location: Missouri, USA
Contact:

Post by BigE »

Doh, misread, I'm doing that a lot today. Maybe I need sleep :) I see what you mean now.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post 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)
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Right I've changed the eval() part so the code now looks like this...

Code: Select all

&lt;?php 
$FINC=mysql_query(" SELECT * FROM fusionengine WHERE func='$INC' "); 

while($thisFINC=mysql_fetch_array($FINC)) 
{ 
$FUSION=$thisFINC&#1111;1]; 
} 

mysql_close($DB); 

$FUSION=addslashes($FUSION); eval($FUSION); $FINC = stripslashes($FUSION); 

echo $FINC; 
?&gt;

But the final result is still being embeded in the HTML and not being executed.

I'm lost for a solution.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe another example helps clearing things up a little
try this one

Code: Select all

&lt;html&gt;&lt;body&gt;&lt;?php


$codeToBeEvaled= &lt;&lt;&lt;EOD
echo '&lt;table border="1"&gt;';
for (\$x=0;\$x!=10; \$x++)
{
	echo '&lt;tr align="center"&gt;';
	for (\$y=0; \$y!=10; \$y++)
		echo '&lt;td width="30"&gt;', \$x*\$y, '&lt;/td&gt;';
	echo '&lt;/tr&gt;';
}
echo '&lt;/table&gt;';
EOD;


echo '&lt;fieldset&gt;&lt;legend&gt;content of $codeToBeEvaled&lt;/legend&gt;&lt;pre&gt;';

echo htmlentities($codeToBeEvaled);

echo '&lt;/pre&gt;&lt;/fieldset&gt;';



echo '&lt;fieldset&gt;&lt;legend&gt;result of $codeToBeEvaled evaluated&lt;/legend&gt;';

eval ( $codeToBeEvaled );

echo '&lt;/fieldset&gt;';
?&gt;&lt;/body&gt;&lt;/html&gt;
Post Reply