Search code examples
phpmysqlfat-free-framework

Parameterized Query Fat Free Framework


I'm having troubles with constructing query's in fat free framework with more than one parameter.

    $result = $db -> exec( array('SELECT * 
        FROM table WHERE table.type = ?', ' OR table.type = ?'), array($id[0],$id[1]));

I get this error : Invalid argument supplied for foreach()

[Z:/web/SITE/lib/base.php:2015] Base->error(500,'Invalid argument supplied for foreach()')

The query works when I test it on the db directly, so that's not the issue.

And to be honest, I don't see any difference with the code shown here :

$db->exec(
    array(
        'DELETE FROM diet WHERE food=:name',
        'INSERT INTO diet (food) VALUES (?)',
        'SELECT * FROM diet'
    ),
    array(
        array(':name'=>'cola'),
        array(1=>'carrot'),
        NULL
    )
);

EDIT Various options that don't work :

$result = $db -> exec( array('SELECT * 
            FROM table WHERE table.type = ? OR table.type = ?'), array($id[0],$id[1]));

$result = $db -> exec( array('SELECT * 
            FROM table WHERE table.type = ?', ' OR table.type = ?' ,$id[0],$id[1]);

This is the example from Fat free framework itself.. Any help is appreciated..


Solution

  • It should be

    $result = $db -> exec(
      'SELECT * FROM table WHERE table.type = ? OR table.type = ?', 
      array(1=>$id[0],2=>$id[1])
    );
    

    When the first parameter is an array, it'll transform into a transaction where each array value from $commands and $args is used for a single query. http://fatfreeframework.com/sql#Transaction