PHP Code in database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

PHP Code in database

Post by anjanesh »

I have this in the database as one of the row entries in the table :
Column is PHPCode
Script1.php?argc1=argv1&argc2=$ID

Code: Select all

$row=mysql_fetch_assoc($res);
$ID=5;
echo $row['PHPCode'];
But this outputs Script1.php?argc1=argv1&argc2=$ID when I want it to output Script1.php?argc1=argv1&argc2=5
Hows that possible ? Any function like parsePHP ?
Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you'll need eval... a better solution would be to change that string to variable inside the string to something else.. like {ID}.. then you can str_replace it with your value.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

This is just one line in this case. What if there is an entire function stored in one of the rows in a MySQL table ?
str_replace() wouldnt help.

Code: Select all

echo $row['PHPCode'];

Code: Select all

function SomeFunc()
{
// ...code
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there generally shouldn't be code stored in the database.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

I quite often store classes in the database and you just run them through eval() once youve grabbed the code and then you can use them. Find its good for auto-update of the users software as you dont have any trouble with file permissions on different systems.
Post Reply