【问题标题】:sql correct but in php it skips the first resultsql 正确,但在 php 中它会跳过第一个结果
【发布时间】:2015-06-13 09:05:42
【问题描述】:

我使用这个:foreach ($result as $row) { $row["testID"], $row["subject"], ... }; 输出所有值。
sql 工作正常,但是当我转到页面时,它总是向我显示所有输出,而没有数组中的第一个元素。
例如,如果我有 5 个测试要显示给用户,它只显示测试 2、3、4 和 5,而不是数组 $result. 中的第一个测试
这是所有代码(抱歉是荷兰语):

require_once( "common.inc.php" );
    require_once( "DataObject.class.php" );

    class StatistiekAanpassen extends DataObject {
        public function toetsen() {
            $conn = parent::connect();

            $order = isset( $_GET["order"] ) ? preg_replace( "/[^ a-zA-Z]/", "", $_GET["order"] ) : "toetsID";

            /*SQL om alle toetsen op te halen*/
            $sql = "SELECT toetsID, onderwerp, puntentotaal, vak 
                    FROM Toets 
                    WHERE NOT EXISTS 
                        (SELECT * 
                        FROM LeerlingToets 
                        WHERE Toets.toetsID = LeerlingToets.toetsID AND 
                        LeerlingToets.gebruikerID = " . $_SESSION["member"]->getValue( "gebruikerID" ) . " ) order by " . $order;

            $result = $conn->query($sql);

            if ($result->fetchColumn()) {
                echo "<table id=\"toetsTable\">";
                ?>
                <tr>
                    <th id="toetsTableIDth"><?php if ( $order != "toetsID" ) { ?><a href="statistiekenAanpassen.php?order=toetsID"><?php } ?>Nr<?php if ( $order != "toetsID" ) { ?></a><?php } ?></th>
                    <th id="toetsTableIDth"><?php if ( $order != "onderwerp" ) { ?><a href="statistiekenAanpassen.php?order=onderwerp"><?php } ?>Onderwerp<?php if ( $order != "onderwerp" ) { ?></a><?php } ?></th>
                    <th id="toetsTableIDth"><?php if ( $order != "vak" ) { ?><a href="statistiekenAanpassen.php?order=vak"><?php } ?>Vak<?php if ( $order != "vak" ) { ?></a><?php } ?></th>
                    <th id="toetsTableIDth"><?php if ( $order != "puntentotaal" ) { ?><a href="statistiekenAanpassen.php?order=puntentotaal"><?php } ?>Puntentotaal<?php if ( $order != "puntentotaal" ) { ?></a><?php } ?></th>
                </tr>
                <?php
                    foreach ($result as $row) {
                        echo "<tr><td class=\"IDtd\"><a href=\"statistiekenAanpassen.php?toetsID=" . $row["toetsID"] . "\">" . $row["toetsID"] . "</a></td>
                    <td><a href=\"statistiekenAanpassen.php?toetsID=" . $row["toetsID"] . "\">" . $row["onderwerp"] . "</a></td>
                    <td><a href=\"statistiekenAanpassen.php?toetsID=" . $row["toetsID"] . "\">" . $row["vak"]. "</a></td>
                    <td><a href=\"statistiekenAanpassen.php?toetsID=" . $row["toetsID"] . "\">" . $row["puntentotaal"]. "</a></td></tr>";
                    }
                echo "</table>";
            } else {
                echo "<p>Er zijn nog geen toetsen.<p>";
                echo "<p>OF<p>";
                echo "<p>U heeft voor alle toetsen al punten ingegeven.<p>";
            }
        }
    }


如何输出所有元素,包括第一个?

【问题讨论】:

  • $result-&gt;fetchColumn()already 很可能已经获取了第一条记录,并使其对您的 foreach 循环“不可用”。
  • 你检查是否有五个结果来了?
  • @VigneshBala,是的,我已经检查过了。例如,我将代码粘贴到 phpmyadmin 中并将会话变量更改为 1,它就可以工作了。
  • @VolkerK 我该如何解决这个问题?

标签: php sql arrays foreach


【解决方案1】:

很可能 $result->fetchColumn() 已经获取了第一条记录,并使其对您的 foreach 循环“不可用”。
您可以使用标志变量来“检查”此迭代是否是第一次,如果是,则打印表头。并且在循环之后通过这个标志检查是否至少打印了一条记录,如果没有打印错误信息。

或者你从一个 for(each) 循环切换到一个 do-while 循环并让循环体打印“前一个”记录的内容,即已经在 if 条件“内”获取的记录或“在”前一次迭代的 while 条件内。

if ( !($row=$result->fetchRow()) ) { // or whatever the method to fetch one record is called
    echo "
        <p>Er zijn nog geen toetsen.<p>
        <p>OF<p>
        <p>U heeft voor alle toetsen al punten ingegeven.<p>
    ";
}
else {
    // <-- put table tag and table header here -->

    do {
        // <-- print the current table row here -->
    }
    while(  $row=$result->fetchRow() ); // again: or whatever the method to fetch one record is called

    // <-- close the table tag here  -->
}

(并且可能有一个 rewind/reset 方法将记录指针设置回开始之前,以便 foreach 将再次遍历第一条记录。)

【讨论】:

    【解决方案2】:

    fetchcolumn 使第一列不可用,请使用

    $result->fetch_assoc()
    

    【讨论】:

    • fetch_assoc() 对我不起作用,但我不知道为什么
    • 然后尝试使用 $results->fetch_object();但是要显示这一点,您需要使用 $row->toetsID; 引用您的列
    • 我需要在哪里参考以及如何参考。抱歉,我对此并不熟悉。
    • 它的 mysqli 方法将结果显示为对象,您只需使用 $results->fetch_object();然后在 foreach 语句中,您将使用 $row->toetsID;显示值而不是 $row[toetsID];
    猜你喜欢
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多