Reham Fahmy
Reham Fahmy

Reputation: 5063

getting mysql results as unique array

if i've the following database lines (id,url,type)

id |  url  | type

1  |google | go
2  |yahoo  | go
3  |google | go
4  |bing   | go

Now i would like to call all urls of type go and get the results into array

$query = "select * from my_table Where type='go'";
$result = mysql_query($query) or die(mysql_error());
while($aax = mysql_fetch_array($result)){

$result_array[] = $aax[url];
$result_array = array_unique($result_array);
print_r($result_array);

}

the resulting array will be repeated 4 times ! as we have 4 results from database

Array
(
    [0] => google
)
Array
(
    [0] => google
    [1] => yahoo
)
Array
(
    [0] => google
    [1] => yahoo
)
Array
(
    [0] => google
    [1] => yahoo
    [3] => bing
)

My question

how i can get the resulting array without repeating and just got all urls so resulting i want is as following

 Array
(
    [0] => google
    [1] => yahoo
    [3] => bing
)

~ any help .. thanks

Upvotes: 0

Views: 962

Answers (2)

Martin
Martin

Reputation: 1243

$result_array = array_merge($result_array, $aax[url]);

Upvotes: 1

JvdBerg
JvdBerg

Reputation: 21856

Use a distinct function on your query, and than just build a normal array:

$query = "select distinct url from my_table Where type='go'";
$result = mysql_query($query) or die(mysql_error());

$result_array = array();

while($aax = mysql_fetch_array($result)){
  $result_array[] = $aax['url'];
}
print_r($result_array);

note: edited some minor bugs in your source.

Upvotes: 4

Related Questions