Jump to content

country list problem


jimmyt1988

Recommended Posts

<?php

    function countryList(){                 
        $numberOfCountries = mysql_query("SELECT * FROM countrylist")
        or die(mysql_error());          
        $result = mysql_num_rows($numberOfCountries);
    
        $countryList = mysql_query("SELECT * FROM countrylist")
        or die(mysql_error());  
        $row = mysql_fetch_array( $countryList );
        
        for ( $i = 0; $i < $result; $i++ ){            
            /*line 45*/echo '<option value = "' . $row[$i] . '">' . $row[$i] . '</option>';
        }
    }
?>

 

is returning:

 


Notice: Undefined offset: 2 in C:\wamp\www\whichcomponent.co.uk\assets\formFunctions.php on line 45

 

not only that.. it actually keeps going until it has one all 239 countries.

 

Why is this?

Link to comment
https://forums.phpfreaks.com/topic/184324-country-list-problem/
Share on other sites

       while ($row = mysql_fetch_array( $countryList )) {
        
        foreach ($row as $value) {            
            echo '<option value = "' . $value . '">' . $value . '</option>';
        }
}

 

I highly doubt that is what you want, I think you are after something like this instead:

 

       while ($row = mysql_fetch_array( $countryList )) {
            echo '<option value = "' . $row[0] . '">' . $row[0] . '</option>';
        }

 

Given that your table has the country name at $row[0], you should really define the columns you want out of the table in the MySQL select statement so you know what is at which index, it will also improve your MySQL  performance.

Link to comment
https://forums.phpfreaks.com/topic/184324-country-list-problem/#findComment-973122
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.