tomtimms Posted April 1, 2010 Share Posted April 1, 2010 I am trying to do a search against my mysql database for names that are "NOT" found. I have a search form that is submitted and I would like to return the names that were not found in the database. Of course I am able to retrieve the names that are found but not the ones that are not. Anyone know the best approach to this? I was thinking to run a comparison of my query to the search variable however I want to know if there is an easier way. Thanks to anyone who an assist. Quote Link to comment https://forums.phpfreaks.com/topic/197190-return-results-not-found-in-a-query/ Share on other sites More sharing options...
MadTechie Posted April 1, 2010 Share Posted April 1, 2010 Find records with the Name MadTechie SELECT * FROM table WHERE name = 'MadTechie' Find records without the Name MadTechie SELECT * FROM table WHERE name != 'MadTechie' Quote Link to comment https://forums.phpfreaks.com/topic/197190-return-results-not-found-in-a-query/#findComment-1035071 Share on other sites More sharing options...
tomtimms Posted April 1, 2010 Author Share Posted April 1, 2010 Ahh I don't think I explained right, When I perform the query, I want it to look for all the values in my array and send back the values that were not found. Quote Link to comment https://forums.phpfreaks.com/topic/197190-return-results-not-found-in-a-query/#findComment-1035573 Share on other sites More sharing options...
MadTechie Posted April 2, 2010 Share Posted April 2, 2010 okay, as this is in the PHP section and not the SQL one try this logic find all items in the database and store them in an array and then use arraydiff to generate a new array of unfound items try this untested script (update table and field of course) <?php $findme = array("One","Two","Three","Four"); $query = sprintf("SELECT field FROM table WHERE field in (%s)",implode(",", $findme)); $result = mysql_query($query); $found = array(); while($row = mysql_fetch_assoc($result)){ $found[] = $row['field']; } $notFound = array_diff($findme, $found); echo "<pre>";var_dump($notFound);echo "</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/197190-return-results-not-found-in-a-query/#findComment-1035581 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.