user3436399
user3436399

Reputation: 77

Menu from SQL database

I am trying to make a drop down menu from a mysql workbench database. My menu is not returning any vales at all. I'm not sure what I am doing wrong, but here is the body of my code.

I am also working on word press, and I my config should be correct or an error would have poped up.

Any help would be greatly appreciated.

enter code 

//connect to db
function connect() {
   mysql_connect (DB_HOST, DB_USER, DB_PASS,DB_NAME) or die ('Could Not Connect to   Database' . mysql_error());

}
//query
$queryCountry = mysql_query("SELECT * FROM pgtpackages.country");

//array
while($arrayCountry = mysql_fetch_array($queryCountry));


?>


<div class="wrap">
    <h3>Country</h3>
    <select name="County">
        <?php foreach($arrayCountry as $option) : ?>
            <option value="<?php echo $option['IDCountry']; ?>"><?php echo    $option['name'] ?>
            </option>
        <?php endforeach; ?>


    </select>
</div>


<?php
}
?>

Upvotes: 0

Views: 72

Answers (2)

Mohammad Ajmal Amirzad
Mohammad Ajmal Amirzad

Reputation: 629

I want to edit your code a little bit which should work for you

<?php
    //your connection to mysql db
    function connect() {
       mysql_connect (DB_HOST, DB_USER, DB_PASS,DB_NAME) or die ('Could Not Connect to   Database' . mysql_error());

    }

    //your query
    $queryCountry = mysql_query("SELECT * FROM pgtpackages.country");

    //array
    while($arrayCountry = mysql_fetch_array($queryCountry));
?>

<div class="wrap">
    <h3>Country</h3>
    <select name="County">
        <?php
            while($arrayCountry = mysql_fetch_array($queryCountry))
            {
        ?>
            <option value="<?php echo $arrayCountry['IDCountry']; ?>"><?php echo $arrayCountry['name']; ?></option>
        <?php 
            }
        ?>
    </select>
</div>

Upvotes: 0

mesutozer
mesutozer

Reputation: 2859

You are consuming all your result set in the following line:

while($arrayCountry = mysql_fetch_array($queryCountry));

You can iterate over the results and print them as follows:

<?php while($arrayCountry = mysql_fetch_array($queryCountry)) : ?>
        <option value="<?php echo $option['IDCountry']; ?>"><?=$option['name']?>
    <?php endwhile; ?>

Upvotes: 2

Related Questions