最后一次回显'd HTML源代码未显示(在'view source'中完全以红色显示)


Last echo'd HTML source not shown (shown completely red in 'view source')

我实际上是在为别人的问题做答案,直到我遇到了一些奇怪的事情。这个问题是关于编写分页系统的。用户希望在他当前的系统中添加一些额外的东西。所以我写了下面的代码:

<?php
// Database Settings
$dbhost = 'localhost';
$dbuser = '';
$dbpass = '';
$dbname = '';
// Establish Connection to the Database
$dbh = new PDO('mysql:host='. $dbhost .';dbname='. $dbname, $dbuser, $dbpass, array(
    PDO::ATTR_PERSISTENT => true,
    PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
));
// Selecting the data from table but with limit
$query = 'SELECT * FROM table_name ORDER BY column_name ASC LIMIT :start, :page';
// Prepare query
$pre = $dbh->prepare($query);
// Binding values
$pre->bindParam(':start', $start_from);
$pre->bindParam(':page', $per_page);
// Results per page
$per_page=2;
if (isset($_GET['page'])) {
    $page = $_GET['page'];
} else {
    $page=1;
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
?>
<!-- Start building HTML table -->
<table>
<?php
// Execute query
try {
    $pre->execute();
    // Fetch all results
    $results = $pre->fetchAll(PDO::FETCH_ASSOC);
    // Loop through results
    foreach($results as $data){
        // Display results in HTML table
        echo "<tr>";
        // Add/Remove your column names here
        echo "<td>". $data['column_name'] ."</td>";
        echo "<td>". $data['column_name'] ."</td>";
        echo "<td>". $data['column_name'] ."</td>";
        // Close HTML table row
        echo "</tr>";
    }
} catch (PDOException $e) {
    echo 'MySQL query error: ' . $e->getMessage();
}
?>
<!-- End building HTML table -->
</table>
<div>
    <?php
    // Now select all data from table
    $query = 'SELECT * FROM users';
    // Prepare the query
    $pre = $dbh->prepare($query);
    // Execute the query
    try {
        $pre->execute();
        // Count the results
        $total_records = $pre->rowCount();
        // Keep a record of total number of rows
        $total_rows = $total_records;
        // Using ceil function to divide the total records on per page
        $total_pages = ceil($total_records / $per_page);
        // Going to first page
        echo "<center><a href='pagination.php?page=1'>First Page</a> ";
        // Showing number of pages in between last page
        for ($i=1; $i<=$total_pages; $i++){
            echo "<a href='pagination.php?page=". $i ."'>". $i ."</a> ";
        }
        // Going to last page
        echo "<a href='pagination.php?page=". $total_pages .">Last Page</a></center> ";
    } catch (PDOException $e) {
        echo 'MySQL query error: ' . $e->getMessage();
    }
    // Calculate first and last item on current page
    $first = $page * $per_page - $per_page;
    $last = $page * $per_page;
    // Showing the results
    echo "<br />";
    echo "Showing ". $first ." to ". $last ." in total record of ". $total_rows;
    ?>
</div>

代码没有返回任何错误,并且似乎显示数据很好。但是,从第112行(// Going to last page下面)回显的所有内容都不会显示在页面上。它在HTML源代码中显示正确的值。但是它在Firefox和Chrome中完全被标记为红色。

我以前从未遇到过这种情况,我在想是什么引起的?

这种问题通常意味着你的HTML格式不佳。

看这一行:

echo "<a href='pagination.php?page=". $total_pages .">Last Page</a></center> ";

>Last Page前缺少一个简单的引号;)

应该是

echo "<a href='pagination.php?page=". $total_pages ."'>Last Page</a></center> ";