gpt4 book ai didi

PHP 代码无法在 HTML 中运行

转载 作者:行者123 更新时间:2023-11-29 11:16:07 25 4
gpt4 key购买 nike

正如所有人评论的那样,我已经更改了我的代码。现在的问题是,当我将下面的 php 代码作为单独的文件运行时,它的运行就像魅力一样:

<?php
require("phpsqlajax_dbinfo.php");

$conn = new mysqli($主机名, $用户名, $密码, $数据库);

$sql = "SELECT username FROM users";
$result = mysql_query($sql);

echo "<select name='username'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] . "'>" . $row['username'] . "</option>";
}
echo "</select>";

?>

像这样:enter image description here

但是当我尝试将其包含到 html 代码中时它不起作用:

<!DOCTYPE html>
<html>
<head>
<title>FusionCharts Column 2D Sample</title>
</head>
<body>
<div>
<?php
require("phpsqlajax_dbinfo.php");

$conn = new mysqli($主机名, $用户名, $密码, $数据库);

$sql = "SELECT username FROM users";
$result = mysql_query($sql);

echo "<select name='username'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] . "'>" . $row['username'] . "</option>";
}
echo "</select>";

?>

</div>
<div id="chart-container">LOADING....</div>
<script src="js/jquery-2.2.4.js"></script>
<script src="js/fusioncharts.js"></script>
<script src="js/fusioncharts.charts.js"></script>
<script src="js/themes/fusioncharts.theme.zune.js"></script>
<script src="js/userChart.js"></script>
</body>
</html>

它给出了空的投递箱: enter image description here

最佳答案

删除 select 中的 select 并且不要将 mysqli_*mysql_* 混合。如下所示: -

<div>
<select>
<?php
require("phpsqlajax_dbinfo.php");

$conn = new mysqli($hostname, $username, $password, $database);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT username FROM users";
$result = $conn->query($query);

?>
<?php
while ($line = $result->fetch_assoc()) {
?>
<option value="<?php echo $line['username'];?>"> <?php echo $line['username'];?> </option>

<?php
}
?>
</select>

</div>

注意:-

文件扩展名必须是 .php 而不是 .html

不要使用(已弃用+删除)mysql_* 库。使用mysqli_*PDO

关于PHP 代码无法在 HTML 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39701510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com