开发者

MySQL Query problem in CodeIgniter

So I'm using the following:

    $r = new Record();
    $r->select('ip, count(*) as ipcount');
    $r->group_by('ip');
    $r->order_by('ipcount', 'desc');
    $r->limit(5);

    $r->get();

    foreach($r->all as $record)
    {
        echo($record->ip." ");
        echo($record->ipcount." <br />");
    }

Standard:

SELECT `ip`, count(*) as ipcount FROM (`soapi`) GROUP BY `ip` ORDER BY `ipcount` desc LIMIT 5;

And I only get the last (fifth) record echo'ed out and no ipcount echoed.

Is there a different way to go around this? I'm working on learning DataMapper (hence the questions) and need to figure some of this out. I haven't quite wrapped my head around the whole ORM thing.

Is there a way to set the count(*) as ipcount without the funny select() statement? I don't think it's triggering for some reason. This could also be a bug in DataMapper, but I'm less certain of that.

Also I found that even if I use the $r->query() method it doesn't return anything except the last entry if I use something like SELECTipFROMsoapiWHERE 1;. It will however return everything (like it should) if I say SELECT * FROM soa开发者_运维百科pi WHERE 1;. If it doesn't have the * it only returns the last line.

Just verified with the new query, anything except selecting all columns (*) only returns the last record. Any help with this would be great. You can craft a statement like select *, count(*) as ipcount but then you still don't have access to it via $record->ipcount.


For your case, once you use COUNT() function in MySQL, it will only return 1 value. Hence you ip result data would not display out.

I suggest you just split this 2 queries. 1. for COUNT(*) 2. for ip

Hope this help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜