Running PHP code from Database

First of all create a Database and run/import the following SQL:

CREATE TABLE `test` (
`id` int(11) NOT NULL auto_increment,
`contents` text collate latin1_general_ci NOT NULL,
PRIMARY KEY  (`id`)
);

INSERT INTO `test` (`id`, `contents`) VALUES
(1, ‘for($i=1;$i<=10;$i++){echo $i.”<br />”;}’);


And then run the following code:

<?php
/**
*
* Running PHP code from Database
*
* @author      Md. Nazmul Basher (nazmul_basher@yahoo.com)
* @Web         https://nazmulb.wordpress.com
* @Mobile        +8801712994064
*/

class runPHP
{
var $server;
var    $username;
var $password;
var $db;
var $link;

function __construct($server, $username, $password, $db)
{
$this->server = $server;
$this->username = $username;
$this->password = $password;
$this->db = $db;
$this->connect();
}

function connect()
{
$this->link = mysql_connect($this->server, $this->username, $this->password);
mysql_select_db($this->db, $this->link);
}

function runPHPfromDB()
{
$query=”select * from test”;
$result = mysql_query($query);
$data = mysql_fetch_array($result);
eval($data[‘contents’]); // Evaluate a string as PHP code
}

function __destruct() {
}

} // End Class

$res = new runPHP(‘localhost’,’root’,”,’test’);
$res->runPHPfromDB();
?>

One Response to Running PHP code from Database

  1. manchumahara says:

    that means eval is the key here.

    thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: