获得行结果的最佳方法,而无需进行多次查询?

时间:2014-05-14 19:27:53

标签: php

来自数据库的其中一个字段由","分隔。如(1,2,3,4,5) 这是我使用的代码:

 $result = mysqli_query($con, "SELECT * FROM login WHERE phone=$user");

 while ($row = mysqli_fetch_array($result)) {
     $friends = $row['friends'];
     $data = $friends;
     $arr = explode(',', $data);
     $today = date("d F", strtotime($row['time']));
     $hour = date('g:i',
     strtotime($row['time']));
     if ($today < $row['time']) {
         $today;
     } else {
         $today = $hour;
     };
     for ($i = 0; $i < count($arr); $i++) {
         $friend = array($data);
         foreach($friend as $value) {
             echo "      
 <div class='contacts_item sep_cont'>
          <img class='user_img' src='".$row['picture']."'>  
          <div class='contacts_title'>".$arr[$i]."<div class='time'>".$today."</div></div>
          <div>".$row['name']."</div>     </div>   ";
         }
     }
 }

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

如果我理解你想要什么,我认为你可以创建一个只包含登录和朋友之间关系的表,如

TABLE login_friends

    id    |    login_id   |   friend_id |
    1     |       1       |      2      |
    2     |       1       |      3      |
    3     |       2       |      3      |