Question: I'm trying to run a search of a photos table based on tags.

I split the search term into individual keywords, and search the table for each individual keyword, pushing any matches to an array.

```
$keywords = explode(' ', $keywords);
$photos = array();

// loop through each keyword
foreach($keywords as $keyword){

$row = $mysqli->query(" SELECT * FROM photos WHERE tags LIKE '%$keyword%' ");

// loop through each row result
    while( $query_result = $row->fetch_assoc() ){

        // push result to photos array
        array_push($photos, $query_result);

    }

}

```

however, this can return the same table row multiple times, e.g if I search for 'mod modded', any photo with a tag of 'modded' would be pushed top the array twice as it matches both keywords.

How do I make sure to only select a row once?

**EDIT**

Thanks for the replies but neither worked, i've achieved what Im after with:

```
$keywords = explode(' ', $keywords);
$photos = array();

// loop through each keyword
foreach($keywords as $keyword){

    $row = $mysqli->query(" SELECT * FROM photos WHERE tags LIKE '%$keyword%' ");

    // loop through each row result
    while( $query_result = $row->fetch_assoc() ){

        $dontPush = false;

        // check photos array to see if the query_result already exists
        foreach($photos as $photo){
            if($photo == $query_result){
                $dontPush = true;
            }
        }

        if($dontPush === false){
            // push result to photos array if it dodesnt already exist
            array_push($photos, $query_result);
        }

    }

}

```

but this creates so many loops that if i have a big database it will surely take a long time to return the results? Is there a better way

Answer: 
Try Select Distinct

```
SELECT DISTINCT * FROM photos WHERE tags LIKE '%$keyword%'

```

[W3 Schools](http://www.w3schools.com/sql/sql_distinct.asp)

Alternatively you can also add LIMIT 1 to the end to return only the first row.
For anyone interested I managed to solve this to make sure no duplicates are returned:

```
$keywords = explode(' ', $keywords);
$photos = array();
$query = " SELECT * FROM photos WHERE tags LIKE ";

// loop through each keyword
foreach($keywords as $i => $keyword){

    $query .= "'%$keyword%'";

    if($i+1 < count($keywords) ){
        $query .= " union SELECT * FROM photos WHERE tags LIKE ";
    }

}

$row = $mysqli->query( $query );

// loop through each row result
while( $query_result = $row->fetch_assoc() ){

    array_push($photos, $query_result);

}

```