Morgan Green
Morgan Green

Reputation: 996

Calling a PHP function from within MySQL

I'm not even sure if this is possible, but essentially I want to have a function stored within my MySQL database, so here I have my database

Database->pages:
[id] [name ] [content ] [enabled] [main] [parent]
[6 ] [login] [login();] [1      ] [0   ] [5     ]

Now I'll have the set returned

public function viewPage() {
        global $db;
        $query = <<<SQL
        SELECT content
        FROM pages
        WHERE id = :getid
SQL;
       $resource = $db->sitedb->prepare( $query );
       $resource->execute( array (
       ':getid'    =>   $_GET['id'],
       ));
       foreach($resource as $row){
               echo $row['content'];
       }
}

Last but not least I have my viewPage.php page that has

$static->viewPage(); 

So when I go to viewPage.php?id=6 I want it to pull the data and since content is login(); I want it to call the login(); function which would be translated into an include file. Is this even possible?

Upvotes: 0

Views: 85

Answers (1)

Muhammad Abdul-Rahim
Muhammad Abdul-Rahim

Reputation: 2010

You can use variable functions to achieve this effect. We would need to verify that the function is_callable beforehand.

Let's say we get a row back with the field name set to login. You can do this:

if( is_callable($row['name']) )
    $row['name']();

This will call the function login. You can also pass parameters if you want, as you would any other function.

Upvotes: 5

Related Questions