Jump to content

Insert Into Array ... Then Display Array


xProteuSx

Recommended Posts

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

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);

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.