Populate dropdown from database
I am creating a custom plugin for our martial arts school and have come up against a problem that is doing my head in. No doubt I am missing something obvious but have never really dealt with WordPress in this capacity before so rather than smash my computer, thought I would ask the brains trust what I am doing wrong 😉
the following code works perfectly and generates the dropdown:
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "testing_db";
$conn = new mysqli($servername, $username, $password, $dbname);
?>
<html>
<body>
<select name="dojang">
<option selected="selected">Choose…</option>
<?php
$sql = mysqli_query($conn, "SELECT dojangName FROM wp_tmadm_dojangs");
while ($row = $sql->fetch_assoc()){
$dojang = $row['dojangName'];
echo "<option value='$dojang'>$dojang</option>";
}
?>
</select>
but the following doesn’t work from within WordPress:
<select name="dojang">
<option selected="selected">Choose…</option>
<?php
global $wpdb;
$sql = $wpdb->mysqli_query("SELECT dojangName FROM wp_tmadm_dojangs");
while ($row = $sql->fetch_assoc()){
$dojang = $row['dojangName'];
echo "<option value='$dojang'>$dojang</option>";
}
?>
</select>
Am I not connecting to the database correctly?
Is there something else I am missing?
Appreciate any help.
Thanks
EDIT: Apologies, I pasted the wrong code in the 2nd block – should have been mysql_query
, not get_results
Leave an answer