Jump to content

CSV file upload to mysql


gregaspen

Recommended Posts

I think I'm close but the script below will not loop... therefore it only writes a single line to the database. Help!

 

--------------------

<?php

 

$databasehost = "localhost";

$databasename = "$db";

$databasetable = "$table";

$databaseusername ="$user";

$databasepassword = "$pass";

$fieldseparator = ",";

$lineseparator = "\n";

$csvfile = $_FILES['dataupload']['tmp_name'];

 

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,$databasepassword) or die(mysql_error());

@mysql_select_db($databasename) or die(mysql_error());

 

$lines = 0;

$queries = "";

$linearray = array();

 

$lineexp = explode($lineseparator,$csvcontent);

 

foreach($lineexp as $line){

$lines++;

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

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

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

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

$linemysql = implode("','",$linearray);

 

$query = "INSERT INTO $databasetable VALUES('$linemysql')";

$upload = @mysql_query($query);

}

 

if ($upload){

header ("location:index.php?count=$lines");

exit();

}

else {

header ("location:index.php?error=1");

exit();

}

 

?>

Link to comment
Share on other sites

if I place

 

echo ($linemysql);

exit();

 

after implode. I only get the first line of data, properly formatted albeit. I should think I would get the entire database with the echo command. Am I on dope? OH WAIT! I am on dope. I should get rid of the exit and then comment out both the $query and $upload lines. Yip, now it shows the entire database... properly formatted, except for the end of line. Maybe that is my problem. But not sure how to fix it. anyone, anyone?

Link to comment
Share on other sites

.CSV file is easy to import into mySQL, its the .XLS file which is a pain!

 

Here is how I import a csv file:

// DUMP From .CSV FILE to mysql
$query = "load data infile '$target_path' into TABLE `my_table_raw` FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' 
IGNORE 1 LINES;";
if (mysql_query($query)) {
// success
}
else{
// error
}

 

 

Once i have the data in raw table, i do all kinds of data verification on that before movig it into a final table.

 

Hope it helps

 

 

.. and why do I have to enter the spam-verfication code ? :S

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.