Andrew
Andrew

Reputation: 243

Need column values in one array on php+mysql.

I am trying to list city names in one array on php-mysql but when I use these lines -->

$sql = "select city from list"; 
$sorgu = mysql_query($sql); 
while ($rs = mysql_fetch_assoc($sorgu)) {
    print_r ($rs); 
} 
mysql_free_result($sorgu); 

Result is:

Array ([0] => ISTANBUL) Array ([0] => LONDON) Array ([0] => PARIS) Array ([0] => OSLO)

It is 4 cities and 4 arrays but I have to list them like that:

Array([0] => ISTANBUL [1] => LONDON [2] => PARIS [3] => NEW YORK)

What should i do?

Upvotes: 1

Views: 68

Answers (1)

MrCode
MrCode

Reputation: 64536

Just append the city to another array:

$array = array();
while ($rs = mysql_fetch_assoc($sorgu)) {
    $array[] = $rs['city'];
} 

print_r($array);

The code in your question will actually output:

Array ([city] => ISTANBUL) Array ([city] => LONDON) Array ([city] => PARIS) Array ([city] => OSLO)

Notice the city key. You would only get the output that you said if you used mysql_fetch_row().


Side note: mysql_* library is deprecated, consider upgrading to PDO or MySQLi.

Upvotes: 4

Related Questions