duanqianruan8448 2015-10-09 21:45
浏览 19
已采纳

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

Hi i am querying my db so that i can compare every two rows. ex 1 and 2, then 2 and 3, then 3 and 4. and so on and so forth. but it only compares the first two rows. any ideas? here is my code:

$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);
}

thanks in advance, have a nice day

  • 写回答

2条回答 默认 最新

  • douwen9534 2015-10-09 23:53
    关注

    Theres a problem right at the while(), you're fetching the 1st row, and is correct. Then, inside it you fetch the 2nd, which is what you intend, but when the iteration is repeating, the while will fetch the 3rd, and the inner the 4th, so you lost there the comparisson between 2nd and 3rd.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)