0

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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.