Lonefish
Lonefish

Reputation: 677

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..

Upvotes: 4

Views: 2172

Answers (2)

Mr Sleeps
Mr Sleeps

Reputation: 31

Try

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

I had a problem with Fat Free and using ? in sql. That solved my problems!

Upvotes: 1

ikkez
ikkez

Reputation: 2052

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

Upvotes: 4

Related Questions