douyanlu7380 2017-08-24 17:56
浏览 214

PHP foreach循环从数据库中按顺序选择记录

I would like to be able to do something like this:

foreach($items as $row) {
   $data = array(
           name = $row->name,
           item = "SELECT section from articles WHERE user_id = 2 LIMIT 1"
    );
}

now my articles table looks like this:

id     user_id  section
=============================
1      2        XYZTRWE
2      2        GWERSTI
3      2        QRTPSLG

The problem is that always very first record from articles gets picked up.

What I would like for example, if I have 2 records in $items array, $data['item'] values should be XYZTRWE and GWERSTI, but I always end up with XYZTRWE for all values.

  • 写回答

1条回答 默认 最新

  • drphfy1198 2017-08-24 18:06
    关注

    You do not need the loop in this instance if you want to view all the records with user_id = 2 in descending order. The SQL statement should be

    "SELECT section FROM articles WHERE user_id = 2 ORDER BY section DESC"
    
    评论

报告相同问题?