如何包含单独的头文件?

How to include a seperate header file?

嘿,我目前正在 html/php 页面上工作。

我有一个头文件,我应该将其包含在我的页面中,但由于某种原因它不起作用而且我不知道,我对所有其他页面都做了同样的事情并且它起作用了所以我不知道为什么突然不行了

<?php
include 'header.php';
?>

<center>
<p>
This page utilizes several postgreSQL method calls.  Such as pg_connect(),
pg_query(), and pg_fetch_result().
</p>
<!-- setup the table -->
<table border="1" width="75%">
    <tr><th width="50%">Make</th><th width="15%">Model</th><th width="20%">Year</th><th width="15%">MSRP</th></tr>
<?php
$output = ""; //Set up a variable to store the output of the loop
//connect
$conn = pg_connect("host=127.0.0.1 dbname=slotegraafd_db user=slotegraafd password=100658347" );

//issue the query
$sql = "SELECT automobiles.make, automobiles.model, automobiles.year, automobiles.msrp
            FROM automobiles
            ORDER BY automobiles.year ASC";
    $result = pg_query($conn, $sql);
    $records = pg_num_rows($result);

//generate the table
    for($i = 0; $i < $records; $i++){  //loop through all of the retrieved records and add to the output variable
        $output .= "\n\t<tr>\n\t\t<td>".pg_fetch_result($result, $i, "Make")."</td>";
        $output .= "\n\t\t<td>".pg_fetch_result($result, $i, "Model")."</td>";
        $output .= "\n\t\t<td>".pg_fetch_result($result, $i, "Year")."</td>";
        $output .= "\n\t\t<td>".pg_fetch_result($result, $i, "msrp")."</td>\n\t</tr>";
    }

    echo $output; //display the output
?>
</table>
<!-- end the table -->
</center>
</body>
</html>

这是我的包含语句的完整代码集。没有开头的 html 或 body 标记,因为它在头文件中,因此我不应该将它添加到此页面中。 有什么帮助吗?

谢谢。

它应该像您的代码一样工作。可能你的header.php路径不对,或者内容无法显示/执行所以什么都没有显示。

确保文件路径正确。如果这个新文件在文件夹中,那么您需要更改 include 语句。

例如,如果这个新文件位于名为 foo 的文件夹中,您可以说:include '/foo/header.php

或者您可以使用相对路径:include ../header.php

如果需要,请阅读文件路径。