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

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Arjun Chaudhary
  • 2,373
  • 2
  • 19
  • 36
  • 3
    Uh. are you having a select inside of another select? Which you appear not to be closing either – Epodax Sep 26 '16 at 11:23
  • you already have an open select in top of the code before printing hi. – Thejas Sep 26 '16 at 11:24
  • You are generating non-valid HTML. – Jan Holas Sep 26 '16 at 11:24
  • 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. – Andrei Sep 26 '16 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. – Epodax Sep 26 '16 at 11:25
  • 2
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** 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](http://php.net/manual/en/book.pdo.php) – RiggsFolly Sep 26 '16 at 11:25
  • I am not sure that showing the ip address here is a good idea too – Thejas Sep 26 '16 at 11:25
  • 1
    @RiggsFolly so you're really using my "A kitten dies" expression for `mysql_*`! – Phiter Sep 26 '16 at 11:26
  • 1
    read mysqli vs mysql – devpro Sep 26 '16 at 11:27
  • @PhiterFernandes Oh yes all over the place, hope you dont mind a bit of plagurism on my behalf – RiggsFolly Sep 26 '16 at 11:27
  • 1
    @RiggsFolly I feel honoured <3 – Phiter Sep 26 '16 at 11:28
  • @PhiterFernandes Cannot remember if yours had a picture of the dastedly deed being done – RiggsFolly Sep 26 '16 at 11:28
  • @PhiterFernandes Its strange how few people comment on it, I had expected more – RiggsFolly Sep 26 '16 at 11:29
  • No no nononono I didn't add an image, I believe the phrase itself is sad enough already. – Phiter Sep 26 '16 at 11:29
  • 1
    Its a bit late to hide the ip address. But you might want to add a password to the root account now as anyone can get into your MYSQL Server Instance now – RiggsFolly Sep 26 '16 at 11:37
  • Guys sorry for all the mistake I will keep all these things in mind next time posting a question. – Arjun Chaudhary Sep 26 '16 at 12:05
  • @ArjunChaudhary Please, read this first. http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php If You have to use deprecated PHP, explain this in Your question and add tags. – Michas Sep 26 '16 at 12:09

1 Answers1

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

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98