4

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

Lonefish
  • 647
  • 2
  • 11
  • 32
  • 1
    Your second example has three different queries. The first one has one, somehow split into two parts. Replace it with something like `$db->exec( 'SELECT * FROM table WHERE table.type = ? OR table.type = ?', $id);`. – raina77ow Sep 15 '15 at 15:34
  • Parameters are ok, remove array – Njuguna Mureithi Sep 16 '15 at 05:02
  • I need the array since I don't know how much types my user will select. I can add OR's to the string in a loop, but I can't even make it work with hardcoded parameters. @raina77ow, thanks. I didn't notice that at first, but in one string it doesn't work either. That was what I tested first.. – Lonefish Sep 16 '15 at 06:57

2 Answers2

4

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

ikkez
  • 2,052
  • 11
  • 20
  • Still no luck "PDOStatement::bindValue(): SQLSTATE[HY093]: Invalid parameter number: Columns/Parameters are 1-based" – Lonefish Sep 16 '15 at 08:43
0

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!

Bilaal Rashid
  • 828
  • 2
  • 13
  • 21
Mr Sleeps
  • 21
  • 2