Okay, here's what I'm doing. I need to take a tab delimited file, read it with PHP and insert it into a database table. Then I need to perform a query, take the result set, and write the results to a file. Worked beautifully with test data but crapped out on the real thing.
Looks like I have some single quotes in the file... example, last name = D'Andre.
Anyway, I need to replace those single quotes with "`". Is there anyway to do an ereg_replace() on the contents of that open file before I export it into the database. Here's what I've got so far. Everything works except for replacing the quotes.
// read the file and insert it into the database
// $fileReadName can be anything like usr/local/bin/testdata.csv
$fileReadName="SEAS_Test.txt";
$handle = fopen($fileReadName, "r");
$oldQuote = "'";
$newQuote = "`";
ereg_replace($oldQuote, $newQuote, $handle);
while (($data = fgetcsv($handle, 1000000, "\t")) !== FALSE) {
$query = "INSERT INTO seas_ftp(`student_number`, `school_id`, `last_name`, `first_name`, `middle_name`, `enroll_status`, `sped_in_progress`, `grade_level`, `gender`, `ethnicity`, `ssn`, `lep`, `DOB`, `street`, `city`, `state`, `zip`, `home_phone`, `mother`, `mother_day_phone`, `mother_home_phone`, `father`, `father_day_phone`, `father_home_phone`, `emerg_contact_1`, `emerg_phone_1`, `entry_date`, `exit_date`, `exit_code`, `next_school`, `sched_next_year_grade`, `spec_ed_stat`, `guardian`, `guardian_day_phone`) VALUES('".$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."', '".$data[4]."', '".$data[5]."', '".$data[6]."', '".$data[7]."', '".$data[8]."', '".$data[9]."', '".$data[10]."', '".$data[11]."', '".$data[12]."', '".$data[13]."', '".$data[14]."', '".$data[15]."', '".$data[16]."', '".$data[17]."', '".$data[18]."', '".$data[19]."', '".$data[20]."', '".$data[21]."', '".$data[22]."', '".$data[23]."', '".$data[24]."', '".$data[25]."', '".$data[26]."', '".$data[27]."', '".$data[28]."', '".$data[29]."', '".$data[30]."', '".$data[31]."', '".$data[32]."', '".$data[33]."')";
$result = mysql_query($query,$conn)or die("Invalid query: " . mysql_error().__LINE__.__FILE__);
}
fclose($handle);
Let me know what you guys think.
Thanks,
Scott