SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jun 2014
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying mysql results in drop down list

    Cant seem to display any data in a drop down list below is my code

    PHP Code:

    <?php
        
        $queryData 
    mysql_query("SELECT SID, StateName FROM State");
        
    $result mysql_fetch_array(mysql_query($queryData));   //$result now has database tables
        
        
    echo '<select name="State" id="State">';
        
        while(
    $row mysql_fetch_assoc($result))
    {

        echo 
    '<option values=' $row["SID"] . '>' $row["StateName"] . '</option>';

    }         
        echo 
    '</select>';
        
        
        
    ?>

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    413
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That doesn't seem quite right to me, I'd think you would want something like:

    PHP Code:
    <?php 
        $queryData 
    mysql_query("SELECT SID, StateName FROM State"); 
        echo 
    '<select name="State" id="State">'
        while(
    $row mysql_fetch_assoc($queryData)) 
          { 
            echo 
    '<option values=' $row["SID"] . '>' $row["StateName"] . '</option>'
            }          
        echo 
    '</select>'
         
    ?>
    But if you're starting out, you really should ditch myqsl calls and use mysqli or PDO instead. Above code assumes you've connected to the database somewhere, and obviously you'd want to check if the query returns something before continuing.
    http://www.firenza.net - my homage to a car from the 1970s

  3. #3
    SitePoint Member
    Join Date
    Jun 2014
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

  4. #4
    SitePoint Member
    Join Date
    Jun 2014
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I have a second drop down list on the same form but the second one is not getting the data as the first on is getting.

    PHP Code:
    <?Php
        $queryState 
    mysql_query("SELECT SID, StateName FROM State");
        echo 
    "<select name='State' id='State' >";
        echo 
    "<option>Select State</option>";  
        while(
    $row mysql_fetch_array($queryState))  
        {
            
                echo  
    '<option value=' $row["SID"] . '>' $row["StateName"] . '</option>';
            
        }
        echo 
    '</select>';
    ?>
                        
                        </td>
                        <td>City</td>
                        <td width="60px">
                        
                            <?php
                            
    $queryCity
    ="SELECT CityName, CID FROM City";
    echo 
    '<select name="City" id="City">'
    echo 
    "<option>Select City</option>"
        while(
    $rs mysql_fetch_array($queryCity))  
        {
              
            echo 
    '<option values=' $rs["SID"] . '>' $rs["CityName"] . '</option>';  
        }  
        echo 
    '</select>';
    //////////////////  This will end the second drop down list ///////////

                            
                            
    ?>
    What is the difference between mysql_fetch_array and mysql_fetch_assoc?

  5. #5
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_fetch_assoc returns results with an associative array (not numerical) while mysql_fetch_array returns both numerical and associative (but has options to choose one or the other too).

    You ought to read this: http://www.php.net/manual/en/mysqlinfo.api.choosing.php and move away from procedural mysql_ calls ASAP. mysqli may be the easiest to move over to as it still allows procedural code or of course PDO for OOP-based.

  6. #6
    SitePoint Member
    Join Date
    Jun 2014
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have solved the problem.

    Quote Originally Posted by gmboat View Post

    PHP Code:

    $queryCity
    ="SELECT CityName, CID FROM City"
    I should have done this:

    PHP Code:
    $queryCity mysql_query("SELECT CityName, CID FROM City"); 

  7. #7
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi gmboat,

    just for an advice,you should not practice native mysql because it is deprecated,you should use PDO,...but if your project using native then you have no choice to continue it..but if you have chance to reverse the code to PDO it's good.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •