xProteuSx Posted December 14, 2008 Share Posted December 14, 2008 I am not very familiar with arrays. I will admit this right off the bat. I have two textbooks on PHP in front of me, and I can't get my code to work. I am trying to add $row[0] from a DB into the array $country_array. This is my code: $country_array = array(); if (mysql_num_rows($countriesresult)>0) { while ($row = mysql_fetch_row($countriesresult)) { echo $row[0]; array_push($country_array, $row[0]); } } echo $country_array; The line 'echo $row[0];' prints all of the countries I have listed in my DB but the code after that is doing one of two things: 1. I am not adding $row[0] to the array properly or 2. You cannot display an array using the echo function Anyone have any suggestions? Thanks. Link to comment https://forums.phpfreaks.com/topic/136922-insert-into-array-then-display-array/ Share on other sites More sharing options...
Mchl Posted December 14, 2008 Share Posted December 14, 2008 2. You cannot display an array using the echo function To display whole array use print_r or var_dump Link to comment https://forums.phpfreaks.com/topic/136922-insert-into-array-then-display-array/#findComment-715071 Share on other sites More sharing options...
DarkWater Posted December 14, 2008 Share Posted December 14, 2008 Don't use array_push(), by the way. It's incredibly slow compared to the native array syntax: <?php if (mysql_num_rows($countriesresult)>0) { while ($row = mysql_fetch_row($countriesresult)) { $country_array[] = $row[0]; } } print_r($country_array); Link to comment https://forums.phpfreaks.com/topic/136922-insert-into-array-then-display-array/#findComment-715080 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.