1

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.

15
  • is the database connection already established in this file? Commented Jul 6, 2017 at 14:08
  • 1
    Warning mysql extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used Commented Jul 6, 2017 at 14:08
  • Can you var_dump() the result? Is it empty? Commented Jul 6, 2017 at 14:08
  • @Ann-SophieAngermüller, yes , my other script are working fine when it comes for insertion and selection .. just this HTML Commented Jul 6, 2017 at 14:09
  • @MaKobi, yeap.. i even tried echoing my $query and no luck. Commented Jul 6, 2017 at 14:09

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

no luck on it too :( , still triggering the no data part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.