PHP代码在HTML中不起作用

时间:2016-09-26 11:22:18

标签: php mysql html5

众所周知,我已经改变了我的代码。现在的问题是当我运行我的下面的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>";

?>

像这样: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($ 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>

它提供空投放箱: enter image description here

1 个答案:

答案 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