众所周知,我已经改变了我的代码。现在的问题是当我运行我的下面的PHP代码作为单独的文件时,其运行就像魅力:
<?php
require("phpsqlajax_dbinfo.php");
$ conn = new mysqli($ hostname,$ username,$ password,$ database);
$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>";
?>
但是当我试图将其包含在html代码中时,它无效:
<!DOCTYPE html>
<html>
<head>
<title>FusionCharts Column 2D Sample</title>
</head>
<body>
<div>
<?php
require("phpsqlajax_dbinfo.php");
$ conn = new mysqli($ hostname,$ username,$ password,$ database);
$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>
答案 0 :(得分:1)
移除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