I need some help with the below PHP/SQL code.
What I'm trying to do is: use a line break delimited text file (containg email address's) and have PHP look up the email address in a mySQL database table (containing email address, email_id, and a user_id column/fields).
Then I want to copy/move the email & the email_id fields to another separate table (email_list_removed) in the same database. And then delete the record from the first table (email_list). And then move on to the next address in the text file, until the end of the file.
For some reason it does the first email address fine (copies it & removes it), but when it gets to the second address, it only copies the email address but not the email_id...it sets the email_id to 0! And it also inserts an empty record inbetween them (in the 2nd database) with an email_id of 0 also...but no info. It looks kinda weird actually...I think it is messing up somewhere with the looping?? -- PLEASE HELP!
[Database table schema: create table email_list(email_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, email VARCHAR(50) NOT NULL, user_id INT NOT NULL); create table email_list_removed(email_id INT NOT NULL PRIMARY KEY, email VARCHAR(50) NOT NULL, user_id INT NOT NULL); ] --I also have created index's of the email column/field in both tables!
HERE IS THE CODE:
<?php
$lines = array();
function fileBreakupLines()
{global $lines;
$file = "removeme.txt";
$fp = fopen($file,"r" or die("Couldn't open $file"
while(!feof($fp))
{$line = fgets($fp,1024);
if(stristr($line,"@")
$lines[] = $line;
else
continue;
}
fclose($fp);
return true;
}
if(fileBreakupLines())
{foreach($lines as $line)
{$dba = array("host","user","passw","dbname"
$link = mysql_connect($dba[0],$dba[1],$dba[2]);
if(!$link)
die("Couldn't connect to mysqld!"
mysql_select_db($dba[3],$link) or die("Couldn't open $dba[3]: ".mysql_error());
$query1 = "LOCK TABLES email_list WRITE, email_list_removed WRITE";
mysql_query($query1,$link);
$query2 = "SELECT * FROM email_list WHERE email='$line'";
$result = mysql_query($query2,$link);
if(!$result)
{$dberror = mysql_error();
print $dberror."<BR><BR>";
print mysql_affected_rows();
}
else
$row = mysql_fetch_array($result);
$query3 = "INSERT INTO email_list_removed(email,email_id) VALUES('$line','$row[email_id]')";
if(!mysql_query($query3,$link))
{$dberror = mysql_error();
print $dberror."<BR><BR>";
print mysql_affected_rows();
}
$rmv_query1 = "DELETE FROM email_list WHERE email='$line'";
if(!mysql_query($rmv_query1,$link))
{$dberror = mysql_error();
print $dberror."<BR><BR>";
print mysql_affected_rows();
}
$query3 = "UNLOCK TABLES";
mysql_query($query3,$link);
}
print mysql_affected_rows();
}
?>
I HOPE SOMEONE CAN HELP ME! - PAINKILLER
What I'm trying to do is: use a line break delimited text file (containg email address's) and have PHP look up the email address in a mySQL database table (containing email address, email_id, and a user_id column/fields).
Then I want to copy/move the email & the email_id fields to another separate table (email_list_removed) in the same database. And then delete the record from the first table (email_list). And then move on to the next address in the text file, until the end of the file.
For some reason it does the first email address fine (copies it & removes it), but when it gets to the second address, it only copies the email address but not the email_id...it sets the email_id to 0! And it also inserts an empty record inbetween them (in the 2nd database) with an email_id of 0 also...but no info. It looks kinda weird actually...I think it is messing up somewhere with the looping?? -- PLEASE HELP!
[Database table schema: create table email_list(email_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, email VARCHAR(50) NOT NULL, user_id INT NOT NULL); create table email_list_removed(email_id INT NOT NULL PRIMARY KEY, email VARCHAR(50) NOT NULL, user_id INT NOT NULL); ] --I also have created index's of the email column/field in both tables!
HERE IS THE CODE:
<?php
$lines = array();
function fileBreakupLines()
{global $lines;
$file = "removeme.txt";
$fp = fopen($file,"r" or die("Couldn't open $file"
while(!feof($fp))
{$line = fgets($fp,1024);
if(stristr($line,"@")
$lines[] = $line;
else
continue;
}
fclose($fp);
return true;
}
if(fileBreakupLines())
{foreach($lines as $line)
{$dba = array("host","user","passw","dbname"
$link = mysql_connect($dba[0],$dba[1],$dba[2]);
if(!$link)
die("Couldn't connect to mysqld!"
mysql_select_db($dba[3],$link) or die("Couldn't open $dba[3]: ".mysql_error());
$query1 = "LOCK TABLES email_list WRITE, email_list_removed WRITE";
mysql_query($query1,$link);
$query2 = "SELECT * FROM email_list WHERE email='$line'";
$result = mysql_query($query2,$link);
if(!$result)
{$dberror = mysql_error();
print $dberror."<BR><BR>";
print mysql_affected_rows();
}
else
$row = mysql_fetch_array($result);
$query3 = "INSERT INTO email_list_removed(email,email_id) VALUES('$line','$row[email_id]')";
if(!mysql_query($query3,$link))
{$dberror = mysql_error();
print $dberror."<BR><BR>";
print mysql_affected_rows();
}
$rmv_query1 = "DELETE FROM email_list WHERE email='$line'";
if(!mysql_query($rmv_query1,$link))
{$dberror = mysql_error();
print $dberror."<BR><BR>";
print mysql_affected_rows();
}
$query3 = "UNLOCK TABLES";
mysql_query($query3,$link);
}
print mysql_affected_rows();
}
?>
I HOPE SOMEONE CAN HELP ME! - PAINKILLER