catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

Duplicate Data Returned in query() with NOT IN (SELECT *) #314

Closed clickbid closed 4 years ago

clickbid commented 9 years ago

I have a standard query that has a NOT IN (select) but when I print_r($dataAry) it gives me duplicate info (see below). Any ideas?

 $dataAry = $db->query("SELECT item_number,item_name,item_type_id 
 FROM items 
 WHERE id 
 NOT IN(
 SELECT items.id AS item_id 
    FROM items 
    JOIN bids ON bids.item_id = items.id 
    WHERE items.user_id = 123
    GROUP BY items.id) 
 AND items.user_id = 123 
 LIMIT 0,6")->fetchAll();

My Results print_r($dataAry):

    {
        "0": "100",
        "1": "Golden Ticket",
        "2": "3",
        "item_number": "100",
        "item_name": "Golden Ticket",
        "item_type_id": "3"
    }...
SyuTingSong commented 9 years ago

You can see the implementation of medoo::query() at here

An instance of PDOStatement would be returned.

According to the manual, you can just pass PDO::FETCH_ASSOC to the fetchAll function so you get the associated array. Or you can pass PDO::FETCH_NUM to the fetchAll function to get the indexed array.

touchweb-vincent commented 9 years ago

In case you would like to use objects, here is the solution i use : $this->pdo->query($query, PDO::FETCH_CLASS, 'stdClass');

But, since Syu point compatibility problems, i'm not sure that it's the right way.

danilodecanini commented 5 years ago

@clickbid I had the same issue and I did what @SyuTingSong said above and it worked for me.

$data = $db->query($query)->fetchAll(PDO::FETCH_ASSOC);

Thanks @SyuTingSong