Using the script below I import a csv file into MySQL.
The csv file I have supplied by my customer has white space as the first character of each line and when using the script below the import only acquires 1 record.
If I manually remove the first character of white space on each line manually and save the file the import works correctly and all records are acquired.
I know it sounds lame but I cannot work out how to edit the csv file and remove the first character of white space prior to importing it.
Could someone please help.
Script cuurently being used:
$myfilename = \"c:\cap\stock.csv\";
if(file_exists (\"$myfilename\"))
{
//empty the old values from the table
$sql = \"DELETE from stock\";
// run SQL against the DB
$result = mysql_query($sql)
or die(\"deletion of stock table failed\");
echo \"<p>\";
}
$lines = file(\"$myfilename\");
foreach ($lines as $line) {
if ($line) {
$line = str_replace(\'\"\', \'\', $line);
$fields = explode(\",\", $line);
//each field in a text file will become a field in MySQL table
$stocknum= $fields[\'0\'];
$description= $fields[\'1\'];
$color= $fields[\'2\'];
$regnum= $fields[\'3\'];
$mileage= $fields[\'4\'];
$keepers= $fields[\'5\'];
$regdate= $fields[\'6\'];
$fsh= $fields[\'7\'];
$opp= $fields[\'8\'];
$siv= $fields[\'9\'];
$prep= $fields[\'10\'];
$age= $fields[\'11\'];
$price= $fields[\'12\'];
$vat= $fields[\'13\'];
$notes= $fields[\'14\'];
$notes2= $fields[\'15\'];
}
Kind regards
Dean