Jump to content

MYSQL Query To Find Results Not In Database


tomtimms

Recommended Posts

I need to have query that will look against my database and then return all the values that were NOT found.  A user searches for the names Greg, Steve, Ben, Joe and the database only has Greg and Steve.  I need to then return the values of Ben and Joe.  Anyone know what would be the best method to do this as?

something along these lines

$names=array("Ben","Tom","George"); 
foreach($names as $name)
{
                      $result=mysql_query("select idfield from yourtable where namefield='".mysql_real_escape_string($name)."'"); 
                      if(mysql_num_rows($result))==0)
                      {
                                $names_not_in_database[]=$name; 
                       }
}

Thanks, however I am having an issue getting my form data to an array.  I have a form field that accepts the names seperated by comma's.  So if you search it would go Ben,Tom,George.  Here is what I have

 

$var = @$_GET['q'];

$trimmed = trim($var);

$trimmed_up = explode(",", $trimmed);

 

foreach($trimmed_up as $name)

{

                      $result=mysql_query("select keyword from site_keywords where keyword='".mysql_real_escape_string($name)."'");

                      if(mysql_num_rows($result)==0)

                      {

                                $names_not_in_database[]=$name;

                                print_r($names_not_in_database);

                               

                      }

}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.