获取数据库中每行的所有行

时间:2013-01-07 12:07:30

标签: php mysql database

我有一个包含成员列表的数据库。我执行某个查询,它选择满足条件的所有成员。然后对于resultset中的每个成员,我想发送在输出中生成的所有成员的详细信息,并继续循环直到结果集结束。我当前的SQL查询是:

if( $result || mysql_num_rows($result) != 0) 
{
    $string = "result";
    while($r = mysql_fetch_array($result,MYSQL_ASSOC))
    {
           while($row = mysql_fetch_array($result,MYSQL_ASSOC))
           {
                  $string .= "," . $row['latitude'] . "," . $row['longitude'];
                  $i = $i + 1;
           }

         **$registration_id = $r['user_id'];**
    }
}

这不会给我一个预期的结果?这个漏洞或漏洞有哪些?

3 个答案:

答案 0 :(得分:0)

第一个while将整行作为数组提取。第二个while毫无意义。

if( $result || mysql_num_rows($result) != 0) 
{
    $string = "result";
    while($r = mysql_fetch_array($result,MYSQL_ASSOC))
    {
        $string .= "," . $r['latitude'] . "," . $r['longitude'];
            $i = $i + 1;

          //Send notification to that member
    }
}

答案 1 :(得分:0)

首先尝试不用

if( $result || mysql_num_rows($result) != 0) 
{
$string = "result";

       while($row = mysql_fetch_array($result,MYSQL_ASSOC))
       {
              $string .= "," . $row['latitude'] . "," . $row['longitude'];
              $i = $i + 1;
       }
      //Send notification to that member
}

答案 2 :(得分:0)

尝试foreach PHP循环

喜欢这个; 查询数据库并根据具体条件构建成员数组,并像这样构建成员数组

$members = array(); // Intialize Empty Array
$sql = "SELECT member_name FROM .... WHERE ... "; // Ensure you're selecting one field here
while($result = $mysql_fetch_assoc(mysql_query($sql)))
{
$members[]=$result['member_name'];//This is the members array we're adding members to
}
//Get Members Array Count
$member_count = count($members);
//Check whether members array is more than 0 
if($member_count != 0)
{
//Do for each loop to select profiles for each member in our array
foreach($members as $key)
  {
  $sql = " SELECT email,number,imei,device,latitude,longitude,userID FROM ... WHERE member_name LIKE '$key' ";
   while($result = $mysql_fetch_assoc(mysql_query($sql)))
      {
        //print results here
        .
        .
        .
      }


  }
}