-4

I'm having a lot of trouble with storing my values from a database into a dropdown list. The code doesn't bring any errors, but it just freezes...

Code

<div class="input-field col-md-12">

          <?php
              echo "<select name='pcID'>";
              $query = "SELECT benutzername FROM benutzer";
              $result = mysql_query($query);
              if(mysql_num_rows($query)>0){
              while($row = mysql_fetch_array($result)){
              echo "<option value=\"owner1\">" . $row['benutzername'] . "
             </option>";
             }
            }
            echo "</select>";
         ?>
        </div>

      </div>

I found a lot of other solutions for my problem. But no solution worked for me. It just freezes up (on my loading screen). And as soon as I remove the PHP part it unfreezes...

Does anyone know how I can solve my problem?

Edit

yea, like I said. I get no error message in the console at all...

2 Answers2

1

After restructuring my code, I solved it by using the PDO statements.

This is how my solutions looks like now:

<div class="input-field col-md-12">

          <?php
              //query
              $sql = "SELECT benutzername FROM benutzer";
              $statement = $pdo->prepare($sql);
              $statement->execute();
              $users = $statement->fetchAll();

          ?>

          <select>
           <?php foreach ($users as $user): ?>
             <option value="<?= $user['id']; ?>"><?= 
             $user['benutzername'];  ?></option>
           <?php endforeach; ?>
         </select>
        </div>
0
  1. You need to connect your db by selecting appropriate database.

    $con=mysql_connect("localhost","root","");
    mysql_select_db("databasename",$con);
    
  2. Your option tag in drop-down list just takes the value and no name to display. I'd suggest you to make minimal changes.

    echo"<option value=".$row["columnname"].">".$row["column2"]."</option>";
    
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
  • 1
    Turn the tide against teaching/propagating sloppy and dangerous coding practices. If you post an answer without prepared statements [you may want to consider this before posting](http://meta.stackoverflow.com/q/344703/). Additionally [a more valuable answer comes from showing the OP the right method](https://meta.stackoverflow.com/a/290789/1011527). – Jay Blanchard Jan 12 '18 at 14:02