Hi all. I'm a relative n00b when it comes to PHP/MySQL, so please excuse me for that. I did a search but did not find too much information, so I'm hoping someone can point me in the right direction.
I'm writing a script to import a CSV file into a MySQL table (nothing fancy), and it works for the most part. Of the 33,000 record file I'm working with, it loads all but ~5 of them. The ~5 it doesn't load have an apostrophe in the field, so for example, if one of the fields is a street address, it's "O'Brian Street" and the ' is causing the MySQL query to fail.
Is there a way I can bypass that MySQL error or force it to accept? I'm guessing this is a simple fix, but any help is greatly appreciated. Thanks in advance.
I'm writing a script to import a CSV file into a MySQL table (nothing fancy), and it works for the most part. Of the 33,000 record file I'm working with, it loads all but ~5 of them. The ~5 it doesn't load have an apostrophe in the field, so for example, if one of the fields is a street address, it's "O'Brian Street" and the ' is causing the MySQL query to fail.
Is there a way I can bypass that MySQL error or force it to accept? I'm guessing this is a simple fix, but any help is greatly appreciated. Thanks in advance.
Code:
$handle = fopen('./Addresses.csv', 'r');
while (($data = fgetcsv($handle, 1000, ',', '"')) !==FALSE)
{
$sql = str_replace("' '", mysql_escape_string("NULL"), $sql);
$query = "INSERT INTO TableName VALUES ('". implode("','", $data)
."')";
$query = @mysql_query($query);
if (mysql_error()) {
echo mysql_error() . "<br>\n";
}
}