Question

Let me start by saying I am completely new to coding and may be diving in way over my head but I am determined to see this through and will get it done.I have been researching questions and watching videos over and over on this subject but none seem to help me and every time I solve one problem another one pops up.( I have learned alot in the past 4 hours though)!

Anyways What I am trying to do is make a simple drop down menu on word press that will be generated from phpmyadmin database tables.

Right now I am getting just an empty box so something must be wrong with my code that accesses my database.I may have deleted something in my multiple outbursts of frustration. (Wish I could post pics... and now that Im looking at my code its about half the size of what I started out with...)

Before I post my code I had a couple of ideas of what could be wrong with this code. 1. My DB_HOST could be wrong possibly? But It worked fine when I took the SQL files from MYSQL workbench to phpmyadmin.

  1. I had trouble earlier working with latin1_swedish_ci collation and maybe my code needs something wtih that?

Anyways thanks for the help!

<?php
/*
Plugin Name: Golf Destinations
*/

ob_start();

    //config 
    define('DB_NAME','***********');
    define('DB_USER','***********');
    define('DB_PASS','***********');
    define('DB_HOST','************');

    //connect to database
    $mysqli = new mysqli (DB_HOST, DB_USER, DB_PASS, DB_NAME);

    //Return an error if bad connection
    if ($mysqli->connect_error) {
        die('Connect Error (' . $mysqli->connect_errno . ')'
            .$mysqli->connect_error);
    }               
    //query database for results
    $query = $mysqli->query("SELECT * FROM 'cities'");         
?>

<h3> City</h3>
<select name="City">
<?php foreach($array as $option) : ?>
    <option value="<?php echo $option->City; ?>">
    </option>
<?php endforeach; ?>
</select>
Was it helpful?

Solution

<select>
<?php
$stmt = $mysqli->prepare($query);
$stmt->execute();
$res = $stmt->get_result();
    while($dropdown = $res->fetch_array(MYSQLI_ASSOC)) {
        echo '<option value="' . $dropdown['city'] . '"></option>';} ?>
</select>

OTHER TIPS

You need to assign your resultset to the array:

<?php foreach($query as $option) : ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top