Medoo icon indicating copy to clipboard operation
Medoo copied to clipboard

medoo::where_clause() issue

Open junpengli opened this issue 13 years ago • 4 comments

Hello,

Thank you for this nice framework.

I hit a problem using the where conditions :

 $where = array(
    "OR" => array(
        "test" => "123",
        "test" => "456",
    )
 );
 $str = $db->where_clause($where);
 echo $str;
 //WHERE test = '456'

I want to get like this:

 //WHERE test = '123' OR test = '456'

junpengli avatar Jul 25 '13 07:07 junpengli

That`s because you cannot declare key-value array with duplicate key name. Not only PHP, but for most programming language.

$data = array(
    'name' => 'foo',
    'name' => 'bar'
);

echo $data['name']; // 'bar'

Any good idea to solve this?

catfan avatar Jul 26 '13 16:07 catfan

My idea : $data = array('name' => array('foo', 'bar'));

On Jul 26, 2013, at 23:45, Catfan [email protected] wrote:

That`s because you cannot declare key-value array with duplicate key name. Not only PHP, but for most programming language.

$data = array( 'name' => 'foo', 'name' => 'bar' );

echo $data['name']; // 'bar' Any good idea to solve this?

— Reply to this email directly or view it on GitHub.

jefrip avatar Jul 26 '13 17:07 jefrip

One solution currently could be:

$database->select("i20", "test", [ "OR" => [ "test" => [ "123" , "456" ] ]]);

results in

SELECT test FROM i20 WHERE test IN ('123','456')

I found a "bug" (?) resulting here:

$database->select("i20", "test", [ "OR" => [ "test" => [123], "test2" => [ 456 ] ]]);

This is what you're looking for:

SELECT test FROM i20 WHERE test IN (123) OR test2 IN (456)

If the columns in where/or-clause are the same, result is strange:

$database->select("i20", "test", [ "OR" => [ "test" => [123], "test" => [ 456 ] ]]); SELECT test FROM i20 WHERE test IN (456)

I think best solution currently is: $database->select("i20", "test", [ "test" => [ "123" , "456" ] ]); results in SELECT test FROM i20 WHERE test IN ('123','456')

fgusinde avatar Sep 04 '13 08:09 fgusinde

Added

catfan avatar Jun 22 '14 17:06 catfan