学习PHP和MySQL。制作了一个简短的脚本,可以从数据库中检索内容,并在我的HTML标题中以标签填充它。
index.php:
<?php
$link = mysqli_connect('localhost', 'root', 'abdullah');
if (!$link) {
echo "Could not connect";
exit();
}
if (!mysqli_select_db($link, 'chitra')){
echo "Could not find database";
exit();
}
$query = 'SELECT * FROM photos;';
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){
$categories[] = $row['category'];
}
include 'main.html';
?>
main.html:
<!DOCTYPE html>
<html>
<head>
<title>Chitra</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<header>
<div id="header_username">username</div>
<div id="header_cat">
<select>
<?php foreach ($categories as $category): ?>
<option><?php echo htmlspecialchars($category, ENT_QUOTES, 'UTF-8'); ?></option>
<?php endforeach; ?>
</select>
</div>
</header>
<main>
TODO
</main>
</body>
</html>
此代码最初是在工作。我开始在身体上工作一段时间,直到突然,它开始向我返回空白的白页。我删除了我的主要内容以从头开始,但它仍然给我相同的结果。<<<<<<<<<<<<<<
我在整个PHP文件中添加了Echo陈述,一切都可以正常执行,直到在Include语句之前为止。我检查了我的Apache,MySQL,PHP安装,检查了所有内容是否启动并运行。一切都是。我无法弄清楚这个问题。
您无法在HTML文件中使用原始PHP功能等尝试将扩展名更改为.php也许对您有帮助。