如何使用数组中的值获取查询结果?

时间:2010-06-29 04:46:17

标签: php mysql

这是我的功能,我从数据库中获取结果

function Prof_Viewer($MemberId)
{
   $query = $this->db->query("SELECT distinct(t1.dProfileId) as prof
   FROM tbl_profile_viewer as t1
   JOIN tbl_login as t2
   WHERE t1.dProfileViwerId='$MemberId'");
if($query->num_rows > 0)
  {
    foreach($query->result() as $row)
     {
       echo  $row->prof;//i am receiving many values here
        $query1 = $this->db->query("SELECT distinct(t3.dUser_name),t2.dPath,t3.dCreatedDate
                                      FROM tbl_login as t3
                                      JOIN tbl_profile_viewer as t1,
                                      tbl_member_details as t2
                                       WHERE t3.dMember_Id = '$row->prof'
                                      AND t2.dMember_Id ='$row->prof'");
     }
      return $query1->result_array();
  }
}

如上所述,我收到许多值,同时回显变量$ row-> prof; 说我有1 2和3等值...... 即使我有这三个值,我'query1'只取最后一个值。所以我只有一个结果我希望查询执行1和2也 如何实现

2 个答案:

答案 0 :(得分:1)

您可以使用PHP explode()将字符串转换为数组。例如:

<?php
$str = 'one|two|three|four';

// positive limit
print_r(explode('|', $str));

// gives you an array:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)

?>

但我认为如果你学会了如何做JOIN会更好:

http://en.wikipedia.org/wiki/Join_(SQL)

答案 1 :(得分:0)

我假设你在这里使用mysqli Result_fetch_array()将执行您想要的操作 http://au2.php.net/manual/en/mysqli-result.fetch-array.php