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

时间:2015-10-09 21:45:20

标签: php mysql database phpmyadmin

您好,我正在查询我的数据库,以便我可以比较每两行。 ex 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);
}

提前感谢,祝你有愉快的一天

2 个答案:

答案 0 :(得分:1)

在while()处遇到问题,你正在取第一行,并且是正确的。 然后,在它内部你获取第二个,这是你想要的,但是当迭代重复时,while将获取第3个,而内部第4个,所以你在第2个和第3个之间丢失了比较。

// 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;
}
  

注意您不应该使用 mysql _ *方法,这些方法已被弃用。你可以使用兄弟 mysqli _ *或PDO

答案 1 :(得分:0)

我会做这样的事情吗?但几乎可以肯定,资源密集程度较低。

$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++;

}