1

I'm a beginner in PHP and I want to display data from a database MySQL, in a drop down list in a page of Wordpress. I used this code:

<tr>
<td> Owner </td>
<td>
<select name="owner">
<?php 
// $connection = mysql_connect(localhost, root, );
$connection = mysql_connect(localhost, root, );

$sql = mysqli_query($connection, "SELECT Nom FROM `herboristes`");

while ($row = $sql->fetch_assoc()){

?>
</select>
<option value="owner1"><?php echo $row['Nom']; ?></option>
</td>
</tr>

The problem is that I get an empty dropdown list as you see in the image below:

image

Please, any suggestions. Thank you!

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
Issam
  • 45
  • 8

4 Answers4

0
while ($row = $sql->fetch_assoc()){

?>
</select>
<option value="owner1"><?php echo $row['Nom']; ?></option>
</td>
</tr>

maybe missing?

<?php } ?>
RzeIMz
  • 50
  • 1
  • 8
0

I really don't know what you want to use? mysql?mysqli? I can't know .. But you shouldn't use mysql_ you can take a look at Why shouldn't I use mysql_* functions in PHP? .. So we'll say you will use mysqli

<tr>
  <td> Owner </td>
  <td>
    <select name="owner">
    <?php 
    // $connection = mysql_connect(localhost, root, );
    $connection = mysqli_connect(localhost, root, );  //mysqli here

    $sql = mysqli_query($connection, "SELECT Nom FROM `herboristes`");

    while ($row = mysqli_fetch_assoc($sql)){   //mysqli here
       echo '<option value="owner1">'. $row["Nom"] .'</option>';  //echo your options here
    }
    ?>
    </select>
  </td>
</tr>
Community
  • 1
  • 1
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
0

try

$connection = mysqli_connect("localhost", "root");

or

$connection = mysqli_connect("localhost", "root", "");
RzeIMz
  • 50
  • 1
  • 8
0

There is no need of writing database connection in wordpress pages instead we can use

    <tr>
    <td> Owner </td>
    <td>
    <select name="owner">
<?php 
        global $wpdb;
        $entries = $wpdb->get_results("SELECT Nom  FROM herboristes");
        foreach( $entries as $entry ) {
    ?>
        <option value="<?php echo  $entry->Nom; ?>"><?php echo  $entry->Nom; ?> </option>
        <?php } ?>

    </select>