Jai
Jai

Reputation: 139

PHP: get php code from DB and run inside a php file only on condition

I have a php file which queries and runs function ('somefunction') on the element fetched from each row.

The issue is now I have a new column which specifies a condition on sql and based on that condition we have to decide whether to run this function or not.

condition has a php code and it has to be executed, sample is given below

<?php   

     $sqlString = 'select * from s_tables';
        $result = mysql_query($sqlString);
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
             $functionName = $row["functionName"];//pre-defined functions inside my php file
             $conditionToCheck = $row["conditionToCheck"];//newly added one, will return a piece of php code
             $stylesToAffect = $row["elementToAffect"];//say html elements H1,H2,P
             if ($conditionToCheck){
                //need to evaluate code which i got from query
                $checkStatus = eval($conditionToCheck);//this is not working
             }

             if ($checkStatus){
                  //this function runs perfectly 
                  if (method_exists($this, $functionName)){
                       $this->$functionName($stylesToAffect);
                  }
             }
        }


        //just for a sample 'conditionToCheck' look like
        $var1 = 2;
        $var2 = 3;
        if ($var1 > $var2){
            $checkStatus = true;
        }else{
            $checkStatus = false;
        }

?>

Can any one look into this, Thanks in prior

Upvotes: 2

Views: 87

Answers (4)

Sagar Guhe
Sagar Guhe

Reputation: 1106

You cannot not execute php code which is stored in database directly. If you wish to do so then you can use eval().

But you might not want to use this if you consider the potential security threats to such implementations.

I would suggest you not to use eval() as if you become a prey to sql injections then this can harm you more.

Update (after reading comments):

In your case you might want to use call_user_func() so your example would be like call_user_func($funtionName, $paramsIfAny); and eval($conditionsToCheck)

Also check your condition code you are not returning/echoing anything so your code would be like (as mentioned by @ravindra-bhalothia):

$var1 = 2;
$var2 = 3;
if ($var1 > $var2){
    $checkStatus = true;
} else {
    $checkStatus = false;
}
return $checkStatus;

Upvotes: 0

Santhy K
Santhy K

Reputation: 839

Try something like this

$var1 = 1;
$var2 = 3;
$conditionCheck = '$var1 > $var2';
$checkStatus = eval( "return $conditionCheck;");
if ($checkStatus) {
    echo "greater";
} else {
    echo "less";
}

Upvotes: 0

Ravindra Bhalothia
Ravindra Bhalothia

Reputation: 1770

If you're using same sample code as you mentioned than you just need to return $checkstatus value.

$var1 = 2;
$var2 = 3;
if ($var1 > $var2){
    $checkStatus = true;
} else {
    $checkStatus = false;
}
return $checkStatus;

Upvotes: 1

user5906831
user5906831

Reputation:

Can you put the condition ??

usually

if

$str =  '<?php echo "test"; ?>';

echo eval('?>'.$str);

will print "test", thus evaluating the expression. Check if this works for you, otherwise put the condition you are evaluating.

Upvotes: 0

Related Questions