开发者

text in cell have ',' uploading .csv is not working

开发者 https://www.devze.com 2023-02-09 10:12 出处:网络
The .csv file i am using to upload data has \',\' in the cell. How can i ignore the \',\' in cells so the data in that cell goes in a single field of my database.

The .csv file i am using to upload data has ',' in the cell. How can i ignore the ',' in cells so the data in that cell goes in a single field of my database.

cell data="There is lead-based paint peeling from the walls, and mold growing on the walls. Summarize the health hazards associated with lead and mold, and describe what may be done to control these two public health problems. Please be sure to support your position with evidence from the literature"

T开发者_开发技巧his text should go in one field but because there is a ',' in the text it is going in 2 fields.

Does anyone know a fix for this.

Thanks

PHP code

$fieldseparator = ",";
$lineseparator = "\n";

$csvfile = "SampleDataTab.txt";
//echo $csvfile;
/********************************/
/* Would you like to add an ampty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************/
$addauto = 0;
/********************************/
/* Would you like to save the mysql queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************/
$save = 1;
$outputfile = "output.sql";
/********************************/
echo $csvfile;

if(!file_exists($csvfile)) {
    echo "File not found. Make sure you specified the correct path.\n";
    exit;
}

$file = fopen($csvfile,"r");

if(!$file) {
    echo "Error opening data file.\n";
    exit;
}

$size = filesize($csvfile);

if(!$size) {
    echo "File is empty.\n";
    exit;
}

$csvcontent = fread($file,$size);

fclose($file);

$con = mysql_connect($databasehost,$databaseusername) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
//'/\s/',
$records=0;
$lines = 0;
$queries = "";
$query="";
$linearray = array();
//array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
foreach(preg_split('/\n/',$csvcontent) as $line) {
echo"<BR>". $line."<BR>";
    $lines++;

    $line = trim($line," \t");

    $line = str_replace("\r","",$line);

    /************************************
    This line escapes the special character. remove it if entries are already escaped in the csv file
    ************************************/
    if($lines==1)
    $line = str_replace("`","\'",$line);
else
    $line = str_replace("'","\'",$line);
    /*************************************/

    $linearray = explode($fieldseparator,$line);


    $stat=  mysql_query("select status_id from status where status='$linearray[3]'");
     $status=mysql_fetch_row($stat);


    $wri=  mysql_query("select user_id from writer where name='$linearray[4]'");
     $writer=mysql_fetch_row($wri);


    $due= date("Y-m-d H:i:s",strtotime($linearray[5]));
    $ord_date=date("Y-m-d H:i:s",strtotime($linearray[6]));

    if($writer[0]==null)
    echo "Please insert writer in database. Cannot add record";
    else
    {
    echo "<BR> category: ".$linearray[13]."<BR>";
if($lines!=1)
{
        $query = "insert into `$databasetable` (website,order_id,status,writer_id,due_date,order_recieve_date,no_of_pages,customer_name,topic,details,category,education_level,format,citation_style,email,alt_email) values('$linearray[0]','$linearray[1]',$status[0],$writer[0],'$due','$ord_date',$linearray[8],'$linearray[10]','$linearray[11]','$linearray[12]','$linearray[13]','$linearray[14]','$linearray[17]','$linearray[18]','$linearray[19]','$linearray[20]');";

    $queries .= $query . "\n";}

echo $query."<BR>";
$records+=  mysql_query($query);

echo $records;

}
}

mysql_close($con);

if($save) {

    if(!is_writable($outputfile)) {
        echo "File is not writable, check permissions.\n";
    }

    else {
        $file2 = fopen($outputfile,"w");

        if(!$file2) {
            echo "Error writing to the output file.\n";
        }
        else {
            fwrite($file2,$queries);
            fclose($file2);
        }
    }

}


If you are using the LOAD DATA INFILE syntax, add ENCLOSED BY '"' to your query.

Docs: http://dev.mysql.com/doc/refman/5.1/en/load-data.html

0

精彩评论

暂无评论...
验证码 换一张
取 消