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?

Link to comment
Share on other sites

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; 
                       }
}

Link to comment
Share on other sites

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);

                               

                      }

}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.