Jump to content

php mysql select col1, col2 from tbl where col1 or col2 in string


antonyfal

Recommended Posts

Hi..

I am working with .csv files.

The app allows a user to import contacts into a database table. I would like to prevent duplicates being added under the users id in the table.

what i need is a function to make sure that the email in a string does/does not match the email in one of two columns IE: email and work email...

 

here is a simple breakdown of my code..

 $nlines = explode("\n",file_get_contents($our_name));  //gets each line from the csv file
    $arr_head = CSV::get_header_fields( $db, $our_name, 'utf8', $seperators, '"', '\\' ); // custom function to get the first line headers
    $num = count($arr_head);     // count the headers as some data columns do not match the headers columns
    $nwFile = fopen($new_file, 'w');
    $newarray ='';
foreach(array_values($nlines) as $value){
 $value = str_replace('"', '', $value);
 $value = str_replace('\'', '', $value);
 $value = str_replace($seperators, ',', $value); // i have a function that gets the seperator, here i change all to a comma
$exists = mysql_query("SELECT email, work_email FROM `contact` WHERE `userid` = '".$User->id."'"); //user id is determined prior
  while($row=mysql_fetch_array($exists)) {
  if(strpos(strtolower(str_replace(',',' ',$value)), strtolower($row['email'])) === false && strpos(strtolower(str_replace(',',' ',$value)), strtolower($row['work_email'])) === false)
  {
  $nnmu = explode(',', $value);
  $totnum = count($nnmu);  // count the value array to see if it is equal to the headers row.
  if($totnum == $num){
   $newarray .= trim($value).','.$thisJRUser->id."\n";
   } else {
   if (preg_match('/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/si', $value))  // if it is not equal to headers row, we take only rows with emails in it and append the difference in columns to the end of the row.
   {
   $nval = $num - $totnum;
   $valfront = split_nth(implode(',',$arr_head),',', $nval);
   $string = preg_replace('~[^,]~','',trim($valfront[0]));   //here we take the cloumns and remove everything  but the comma
   $newarray .= trim($value).',,'.$string.',,,'.$thisJRUser->id."\n";   // here we add the extra columns to the short value string + 5 extra commas as we are always 5 short :-) it works perfectly..
   }
   }
   }
   }
}
 
fwrite($nwFile,$newarray); here we write it all to a new csv file named after userid..
fclose($nwFile);

 

problem im having with this function is that it repeats the header row before it shows the second row of data... so i have 2 headers and data..

 

Can anyone see my error, or have a cleaner solution to this?

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.