0

我有一个包含成员列表的数据库。我执行某个查询,它选择满足条件的所有成员。然后对于结果集中的每个成员,我想发送输出中生成的所有成员的详细信息并继续循环直到结果集结束。我当前的 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'];**
    }
}

这没有给我预期的结果?这有什么漏洞或错误吗?

4

3 回答 3

0

尝试不先 WHILE

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
}
于 2013-01-07T12:15:02.930 回答
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
    }
}
于 2013-01-07T12:13:36.427 回答
0

尝试foreachPHP 循环

像这样; 查询数据库并在给定特定条件的情况下构建成员数组并像这样构建成员数组

$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
        .
        .
        .
      }


  }
}
于 2013-01-20T09:52:34.237 回答