FreedomPride
FreedomPride

Reputation: 1102

HTML Select option with PHP Dynamic

I'm getting a weird result when i try to implement the PHP inside a HTML

The config is literally my DB connection, other scripts work well but only for this matter i couldn't figure out.
Maybe I missed out some elements.

<select name="country">
    <option value="" disabled selected style="display: none;">All Japan Cities</option>
    <?php
    include 'scripts/config.php';
        $query = "SELECT state FROM product";
        $result = mysql_query($query);
        $count = count($result);
        if (!empty($count)) {
            while($row = mysql_fetch_array($result))
            {              
                $state = $row['state'];                                                
                echo "<option value='$state'> $state </option>";
            }
        } else {
            echo '<option>No data</option>';
        }
    ?> 
    </select>

I keep on getting no data for my select statement where I have 3 results in my db.

Upvotes: 1

Views: 117

Answers (1)

Eric Petroelje
Eric Petroelje

Reputation: 60498

I don't think you can do a count() on a mysql result set like that.

Try using mysql_num_rows instead, like this:

....
$count = mysql_num_rows($result);
    if (!empty($count)) {
....

Also, as others have said, these old mysql_ functions are deprecated, so you should probably switch to mysqli or PDO if that is practical as well.

Upvotes: 2

Related Questions