数据库查询,比较每两行但只得到前两行

database query, compare each two rows but getting only first two rows

您好,我正在查询我的数据库,以便我可以比较每两行。例如 1 和 2,然后是 2 和 3,然后是 3 和 4,依此类推。但它只比较前两行。有任何想法吗?这是我的代码:

$result = mysql_query("SELECT *FROM attendance ORDER BY pid ASC") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // $response["nominees"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $prev_sid = $row["sid"];
        $prev_pid = $row["pid"];

        $row2 = mysql_fetch_array($result);
            if($row2["pid"] == $prev_pid){
                if(($row2["sid"] - $prev_sid) == 1){

                    $attended_elec = mysql_query("SELECT pid FROM election_attendance where election_id = '$election_id'");

                    if(mysql_num_rows($attended_elec) > 0){

                        $not_officer = mysql_query("SELECT usertype FROM users WHERE pid = '$prev_pid'");
                        if(mysql_result($not_officer, 0) == "member"){
                            // echo "PID" . $prev_pid;
                            // $nominee["pid"] = $row2["pid"];
                            $user_details = mysql_query("SELECT *FROM users WHERE pid = '$prev_pid'");

                            if(mysql_num_rows($user_details) > 0){
                                $response["nominees"] = array();

                                while ($row3 = mysql_fetch_array($user_details)) {
                                    $nominee = array();
                                    $nominee["pid"] = $row3["pid"];
                                    $nominee["firstname"] = $row3["firstname"];
                                    $nominee["lastname"] = $row3["lastname"];
                                    $nominee["gender"] = $row3["gender"];
                                    $nominee["contact"] = $row3["contact"];
                                    $nominee["email"] = $row3["email"];
                                    $nominee["address"] = $row3["address"];
                                    $nominee["institution"] = $row3["institution"];
                                    $nominee["points"] = $row3["points"];
                                    $nominee["usertype"] = $row3["usertype"];

                                    // push single product into final response array
                                    array_push($response["nominees"], $nominee);
                                }
                            }
                        }
                    }
                }
            } 
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);


} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "There is no candidate yet from the records.";

    // echo no users JSON
    echo json_encode($response);
}

提前致谢,祝您愉快

我会做这样的事情吗?但是几乎可以肯定有一种资源密集度较低的方法来做到这一点。

$x = 0;
$y = 1;
$total = mysql_num_rows(mysql_query("SELECT * FROM `the_place`");

while ($x < $total AND $y < total){

  $query1 = "SELECT * FROM `the_place` LIMIT $x,1";
  $query2 = "SELECT * FROM `the_place` LIMIT $y,1";

  $row1 = mysql_fetch_array(mysql_query($query1);
  $row2 = mysql_fetch_array(mysql_query($query2);

  // Do comparison here
  if ($row1 == $row2){ 
     // etc
  }

  $x = $x++;
  $y = $y++;

}

while() 出现问题,您正在获取第 1 行,并且是正确的。 然后,在它里面你获取第二个,这是你想要的,但是当迭代重复时,while 将获取第三个,而内部获取第四个,所以你失去了第二个和第三个之间的比较。

// fetch data from DB
$results = "...";

if (mysql_num_rows($result) < 1) 
{
    // no products found tell your users
    return;
}

// lets make some variables to walk through the list
$previous = mysql_fetch_array($results);
$actual = null;

// and now lets walk through the list
while($actual = mysql_fetch_array($results))
{
    // execute your logic here

    // than at the end move the actual row to become the previous
    $previous = $actual;
}

NOTICE you shouldn't use mysql_ * methods they're are deprecated. you can use the brother mysqli_ * or PDO