0

As all have commented I have changed my code. Now the thing is when I am running my below php code as separate file its running like charm:

<?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>";

?>

Like this: enter image description here

But when I am trying to include this into html code its not working:

<!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>

Its giving empty drop box: enter image description here

17
  • 3
    Uh. are you having a select inside of another select? Which you appear not to be closing either Commented Sep 26, 2016 at 11:23
  • 1
    new mysqli... -> mysql_query(...). Not the same. How are you not getting errors? And stop using mysql_* they've been deprecated for such a long time it's not even funny anymore. Commented Sep 26, 2016 at 11:24
  • 1
    Your code is filled with errors, do learn some basic HTML / php syntax, you can't mix and match mysql / mysqli either. Commented Sep 26, 2016 at 11:25
  • 2
    Every time you use the mysql_ database extension in new code a Kitten is strangled somewhere in the world it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions. Start here Commented Sep 26, 2016 at 11:25
  • 1
    @RiggsFolly so you're really using my "A kitten dies" expression for mysql_*! Commented Sep 26, 2016 at 11:26

1 Answer 1

1

Remove select inside select and don't mix mysqli_* with mysql_*.Do like below:-

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

Note:-

file extension must be .php not .html.

Don't use (deprecated + removed) mysql_* library. Use mysqli_* or PDO

Sign up to request clarification or add additional context in comments.

2 Comments

Hey anant can you just keep the Note:- part intact. Because that was the mistake and then i ll accept the answer. Thanks I just started learnin php so didn't know abt it.
The error was using mysqli and naming the file with .html extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.