在文档的其他部分使用变量


Use variable in other part of the document

我想在表中回显变量$row。但它并没有真正起作用。$row不是全球性的吗?我认为变量只有在函数内部时才是局部的。 ...

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo $row["teacherID"] . " " . $row["shorthand"] . " " . $row["votes"]. "<br>";
    }
} else {
    echo "0 results";
}
...
<td><?php echo $row["shorthand"]; ?></td>

您可能希望执行以下操作:

if ($result->num_rows > 0) {
    // output data of each row
    echo "<table>";
    while($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>" . $row["teacherID"] . "</td>";
        echo "<td>" . $row["shorthand"] . "</td>";
        echo "<td>" . $row["votes"]. "</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}

如果您的表中有其他行/列,您应该能够弄清楚如何将它们放入其中