Welcome Guest, Not a member yet? Register   Sign In
Missing results using where_in
#1

[eluser]timpiele[/eluser]
I am running this very simple query against a 'products' table:

Code:
$ids = "1003,1005,1007,1009,1011,1013,1015,1017,1019,1021";

$this->db->select("id, title");
$this->db->where_in('id', $ids);
$this->db->limit(10);
$query = $this->db->get("products")->result();

$this->db->last_query() shows me:

Code:
SELECT `id`, `title` FROM (`products`) WHERE `id` IN ('1003,1005,1007,1009,1011,1013,1015,1017,1019,1021') LIMIT 10

I have products in that table with ID's from 1001 through 1025, so they should return.

print_r($query) gives me:

Code:
Array ( [0] => stdClass Object ( [id] => 1003 [title] => Vanilla ) )

Any thoughts on why it is just picking up the first item and that's it?

I have tried single and double quoting each id in the $ids string, and putting double quotes inside of the IN() clause but I still get one result.

if I comment out the where_in() clause I get the following, so I know the data is there:

Code:
Array
(
    [0] => stdClass Object
        (
            [id] => 1001
            [title] => Sweet Caroline
        )

    [1] => stdClass Object
        (
            [id] => 1002
            [title] => Chocolate
        )

    [2] => stdClass Object
        (
            [id] => 1003
            [title] => Vanilla
        )

    [3] => stdClass Object
        (
            [id] => 1004
            [title] => Mocha Fudge
        )

    [4] => stdClass Object
        (
            [id] => 1005
            [title] => Mint
        )

    [5] => stdClass Object
        (
            [id] => 1006
            [title] => Irish Cream
        )

    [6] => stdClass Object
        (
            [id] => 1007
            [title] => Cinnamon
        )

    [7] => stdClass Object
        (
            [id] => 1008
            [title] => Cappuccino
        )

    [8] => stdClass Object
        (
            [id] => 1009
            [title] => Frost Fire
        )

    [9] => stdClass Object
        (
            [id] => 1010
            [title] => Carrot Cake
        )

)
#2

[eluser]Ckirk[/eluser]
I think it's because the where_in function is placing quotes around the $ids array
There shouldn't be quotes in there for an array of numbers.

I haven't tested it but try putting in the SQL yourself with quotes and then without. I'm fairly sure that's the problem. It's not the solution though


[EDIT]
quick search on google found someone else having a similar problem
#3

[eluser]Ckirk[/eluser]
Actually the answer should be simpler than I first thought (it's nearly 3am here so am tired).
It's not the where_in function that's putting quotes on it. It's you.

change
Code:
$ids = "1003,1005,1007,1009,1011,1013,1015,1017,1019,1021";

to
Code:
$ids = array(1003,1005,1007,1009,1011,1013,1015,1017,1019,1021);


That should solve it.
#4

[eluser]Pert[/eluser]
If ids are stored as a string, you can also do
Code:
$ids_array = explode(',', $ids);




Theme © iAndrew 2016 - Forum software by © MyBB