通过 PHP、Select 调用 SQL 所有命令但缺少 1 个结果?

Calling SQL through PHP, Select all command but is missing 1 result?

所以基本上,我有一个 PHP 网页使用 SQL 检查是否有任何 id 被 selected。如果不是,它会自动 selects 所有日期为 2015 的结果。但是第一个结果总是丢失。通过阅读其他帖子,我只能假设我的 select all $result 从 1 开始,即使 table 中的数据从 0 开始。要么,要么我的代码的某些部分正在吃东西第一个结果。如有任何帮助,我们将不胜感激。

if (isset($_GET["id"])){$id=$_GET["id"];}else{$id="";}

if ($id==""){
$select ="select * from organicgrants where lastupdated > '2015'";

$result = mysql_query($select);
if (!$result) {
    echo("<P>Error performing query: " .
       mysql_error() . "</P>");
    exit();
}   
while ( $row = mysql_fetch_array($result) ) {
    $organicgrantsid=$row["organicgrantsid"];
    $firstname=$row["teacherfirstname"];
    $lastname=$row["teacherlastname"];
    $lastupdated=date('F j, Y', strtotime($row["lastupdated"]));

    echo ("<p>");                       
    echo("<p><b>Total Applications Displayed: ".mysql_num_rows($result)."</b></p>");
    echo ("<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">");
    echo ("<tr bgcolor=\"#cccccc\"><td><Strong>Teacher Name</td><td><Strong>School Name</td><td><Strong>State</td><td><Strong>Grade(s)</td><td><Strong>Submitted Date</td>
    <td><Strong>Status</td></Strong></tr>");

while ( $row = mysql_fetch_array($result) ) {;
    $organicgrantsid=$row["organicgrantsid"];
    $teacherfirstname=$row["teacherfirstname"];
    $teacherlastname=$row["teacherlastname"];
    $schoolname=$row["schoolname"];
    $schoolstate=$row["schoolstate"];
    $gradelevels=$row["gradelevels"];
    $lastupdated=$row["lastupdated"];
    $grantstatus=$row["grantstatus"];
    //$staffawarded=$row["staffawarded"];
    //$staffconference=$row["staffconference"];

switch ($grantstatus){
    case "":
        $grantstatus="Waiting List";
    break;
    case "0":
        $grantstatus="Waiting List";
    break;
    case "1":
        $grantstatus="Awarded";
    break;
    case "2":
        $grantstatus="Not Awarded";
    break;
}

    $lastupdated=date('F j, Y', strtotime($row["lastupdated"]));
    echo ("<tr><td><a href=\"results.php?id=".$organicgrantsid."\">".$teacherfirstname." ".$teacherlastname."</a></td><td>".$schoolname."</td><td>".$schoolstate."</td><td>".$gradelevels."</td><td>(".$lastupdated.")</td><td>".$grantstatus."</td></tr>");

}
echo ("</table>");
}
}else{

$select ="select * from organicgrants where organicgrantsid=$id";

$result = mysql_query($select);
    if (!$result) {
    echo("<P>Error performing query: " .
       mysql_error() . "</P>");
    exit();
    }   
    while ( $row = mysql_fetch_array($result) ) {
    $schoolname=$row["schoolname"];
    $schooladdress=$row["schooladdress"];
    $schoolcity=$row["schoolcity"];
    $schoolstate=$row["schoolstate"];
    $schoolzip=$row["schoolzip"];
    $schoolphone=$row["schoolphone"];
    $schoolcounty=$row["schoolcounty"];
    $gradelevelsitem=$row["gradelevels"];
    $teacherfirstname=$row["teacherfirstname"];
    $teacherlastname=$row["teacherlastname"];
    $teacherposition=$row["teacherposition"];
    $teacheremail=$row["teacheremail"];
    $teacherphone=$row["teacherphone"];
    $numberofstudents=$row["numberofstudents"];
    $budgetA=$row["budgetA"];
    $budgetB=$row["budgetB"];
    $budgetC=$row["budgetC"];
    $budgetD=$row["budgetD"];
    $budgetE=$row["budgetE"];
    $budgetF=$row["budgetF"];
    $budgetG=$row["budgetG"];
    $budgetI=$row["budgetI"];
    $budgetJ=$row["budgetJ"];
    $budgetK=$row["budgetK"];
    $budgetAamt=$row["budgetAamt"];
    $budgetBamt=$row["budgetBamt"];
    $budgetCamt=$row["budgetCamt"];
    $budgetDamt=$row["budgetDamt"];
    $budgetEamt=$row["budgetEamt"];
    $budgetFamt=$row["budgetFamt"];
    $budgetGamt=$row["budgetGamt"];
    $budgetHamt=$row["budgetHamt"];
    $budgetIamt=$row["budgetIamt"];
    $budgetJamt=$row["budgetJamt"];
    $budgetKamt=$row["budgetKamt"];
    $budgetLamt=$row["budgetLamt"];
    $budgetMamt=$row["budgetMamt"];
    $essayimportant=$row["essayimportant"];
    $essaybenefit=$row["essaybenefit"];
    $essayexperience=$row["essayexperience"];
    $essaybeneficial=$row["essaybeneficial"];
    $supportingfunds=$row["supportingfunds"];
    $principalname=$row["principalname"];
    $principalemail=$row["principalemail"];
    $grantstatus=$row["grantstatus"];
    $w9status=$row["w9status"];
    $agreementstatus=$row["agreementstatus"];
    $progressstatus=$row["progressstatus"];
    $finalstatus=$row["finalstatus"];
    $receiptsstatus=$row["receiptsstatus"];
    $staffstatus=$row["staffstatus"];
    $imisstatus=$row["imisstatus"];

    $schooladmin=$row["schooladmin"];
    if ($schooladmin==1){$schooladmin="Yes";}else{$schooladmin="No";}
    $w9form=$row["w9form"];
    if ($w9form==1){$w9form="Yes";}else{$w9form="No";}
    $gradelevelsitem="";
    if (isset($_POST['gradelevels'])==true){$gradelevels=$_POST['gradelevels'];foreach ($gradelevels as $key => $value )$gradelevelsitem.="$value, ";} 
    $gradelevelsitem=substr($gradelevelsitem,0,(strlen($gradelevelsitem)-2));`

两个 while 循环的目的是什么(除了窃取你的第一个 row – 在 内部循环 你重新分配 $row 来自外循环第一次迭代的变量)?移动第一个 while 循环中的所有 assignments/output 并删除第二个循环(您缺少大括号 {} 所以我猜这是一个内部循环)。

而不是这个:

while ( $row = mysql_fetch_array($result) ) {
    $organicgrantsid=$row["organicgrantsid"];
    $firstname=$row["teacherfirstname"];
    $lastname=$row["teacherlastname"];
    $lastupdated=date('F j, Y', strtotime($row["lastupdated"]));

    echo ("<p>");                       
    echo("<p><b>Total Applications Displayed: ".mysql_num_rows($result)."</b></p>");
    echo ("<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">");
    echo ("<tr bgcolor=\"#cccccc\"><td><Strong>Teacher Name</td><td><Strong>School Name</td><td><Strong>State</td><td><Strong>Grade(s)</td><td><Strong>Submitted Date</td><td><Strong>Status</td></Strong></tr>");

    while ( $row = mysql_fetch_array($result) ) {;
        $organicgrantsid=$row["organicgrantsid"];
        $teacherfirstname=$row["teacherfirstname"];
        $teacherlastname=$row["teacherlastname"];
        $schoolname=$row["schoolname"];
        $schoolstate=$row["schoolstate"];
        $gradelevels=$row["gradelevels"];
        $lastupdated=$row["lastupdated"];
        $grantstatus=$row["grantstatus"];
        //$staffawarded=$row["staffawarded"];
        //$staffconference=$row["staffconference"];

        switch ($grantstatus){
        case "":
            $grantstatus="Waiting List";
        break;
        case "0":
            $grantstatus="Waiting List";
        break;
        case "1":
            $grantstatus="Awarded";
        break;
        case "2":
            $grantstatus="Not Awarded";
        break;
        }

        $lastupdated=date('F j, Y', strtotime($row["lastupdated"]));
        echo ("<tr><td><a href=\"results.php?id=".$organicgrantsid."\">".$teacherfirstname." ".$teacherlastname."</a></td><td>".$schoolname."</td><td>".$schoolstate."</td><td>".$gradelevels."</td><td>(".$lastupdated.")</td><td>".$grantstatus."</td></tr>");
    }
    echo ("</table>");
}

使用这个:

echo ("<p>");                       
echo("<p><b>Total Applications Displayed: ".mysql_num_rows($result)."</b></p>");
echo ("<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">");
echo ("<tr bgcolor=\"#cccccc\"><td><Strong>Teacher Name</td><td><Strong>School Name</td><td><Strong>State</td><td><Strong>Grade(s)</td><td><Strong>Submitted Date</td><td><Strong>Status</td></Strong></tr>");

while ( $row = mysql_fetch_array($result) ) {
    $organicgrantsid=$row["organicgrantsid"];
    $firstname=$row["teacherfirstname"];
    $lastname=$row["teacherlastname"];
    $lastupdated=date('F j, Y', strtotime($row["lastupdated"]));
    $organicgrantsid=$row["organicgrantsid"];
    $teacherfirstname=$row["teacherfirstname"];
    $teacherlastname=$row["teacherlastname"];
    $schoolname=$row["schoolname"];
    $schoolstate=$row["schoolstate"];
    $gradelevels=$row["gradelevels"];
    $lastupdated=$row["lastupdated"];
    $grantstatus=$row["grantstatus"];
    //$staffawarded=$row["staffawarded"];
    //$staffconference=$row["staffconference"];

    switch ($grantstatus){
        case "":
            $grantstatus="Waiting List";
        break;
        case "0":
            $grantstatus="Waiting List";
        break;
        case "1":
            $grantstatus="Awarded";
        break;
        case "2":
            $grantstatus="Not Awarded";
        break;
    }

    $lastupdated=date('F j, Y', strtotime($row["lastupdated"]));
    echo ("<tr><td><a href=\"results.php?id=".$organicgrantsid."\">".$teacherfirstname." ".$teacherlastname."</a></td><td>".$schoolname."</td><td>".$schoolstate."</td><td>".$gradelevels."</td><td>(".$lastupdated.")</td><td>".$grantstatus."</td></tr>");        
}
echo ("</table>");