Please login or register.

Login with username, password and session length
Advanced search  

News:

Get quality web hosting, virtual private servers, reseller web hosting, and dedicated servers from www.webhostfreaks.com or www.serverpowered.com!

Maintenance Notice

PHPFreaks has successfully moved to a new Dedicated Server, hosted by Server Powered. Please help support future upgrades by Donating.

Author Topic: large csv file to mysql using php?  (Read 349 times)

0 Members and 1 Guest are viewing this topic.

paulmo

  • Enthusiast
  • Offline Offline
  • Posts: 74
    • View Profile
large csv file to mysql using php?
« on: November 16, 2008, 10:03:02 AM »
trying to load maxmedia geocity lite's (free) large csv files using a blogger's php code (have x'd values). i've ftp uploaded this file to server, and created link to it from webpage, thinking that would "activate" it somehow (i'm a newbie). in phpadmin/mysql, i'm creating "csv" table with 1 field and naming it "blocks" although that's probably not the right thing to do (would rather have the code populate/make the table, but don't know how to do that). anyhow the csv file is freezing/not loading through import.

using php/mysql on local machine was a headache previously so i'm doing all of this via remote server (ftp/phpadmin/mysql=godaddy).

please help, thanks.
Code: [Select]
$databasehost = “xxx”;
$databasename = “xxx”;
$databasetable = “csv”;
$databaseusername =”xxx”;
$databasepassword = “xxx”;
$fieldseparator = “,”;
$lineseparator = “\n”;
$csvfile = “GeoLiteCity-Blocks.csv”;
/********************************************************************************************/
/* 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”;
/********************************************************************************************/

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

$lines = 0;
$queries = “”;
$linearray = array();

foreach(split($lineseparator,$csvcontent) as $line) {

$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
***********************************************************************************************/
$line = str_replace(”‘”,”\’”,$line);
/**********************************************************************************************/

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

$linemysql = implode(”‘,’”,$linearray);

if($addauto)
$query = “insert into $databasetable values(”,’$linemysql’);”;
else
$query = “insert into $databasetable values(’$linemysql’);”;

$queries .= $query . “\n”;

@mysql_query($query);
}

@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);
}
}

}

echo “Found a total of $lines records in this csv file.\n”;

?>
Logged

Mchl

  • Supporter
  • Addict
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 2,402
  • That's Largo in my avatar, not me.
    • View Profile
    • WWW
Re: large csv file to mysql using php?
« Reply #1 on: November 16, 2008, 10:07:12 AM »
Try LOAD DATA INFILE syntax
Logged

PHP 5.2.6 | MySQL 5.1.30
Input queue: sqlite, Zend Framework, PostgreSQL
NetBeans fanatic | ExtJS masochist
I'm true neutral. I go both ways.

paulmo

  • Enthusiast
  • Offline Offline
  • Posts: 74
    • View Profile
Re: large csv file to mysql using php?
« Reply #2 on: November 16, 2008, 10:30:47 AM »
thanks, how to do that please?
Logged

Mchl

  • Supporter
  • Addict
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 2,402
  • That's Largo in my avatar, not me.
    • View Profile
    • WWW
Re: large csv file to mysql using php?
« Reply #3 on: November 16, 2008, 10:32:00 AM »
MySQL manual has some info
Logged

PHP 5.2.6 | MySQL 5.1.30
Input queue: sqlite, Zend Framework, PostgreSQL
NetBeans fanatic | ExtJS masochist
I'm true neutral. I go both ways.

paulmo

  • Enthusiast
  • Offline Offline
  • Posts: 74
    • View Profile
Re: large csv file to mysql using php?
« Reply #4 on: November 16, 2008, 10:45:18 AM »

ok i found the page:
http://dev.mysql.com/doc/refman/5.0/en/load-data.html

guessing i do that in the "sql query" box?

since csv file is on my computer, how is this loaddata method going to get the file from my computer? there's no mention of an upload method here; it seems to be written as if the file is already in the database.
Logged

Mchl

  • Supporter
  • Addict
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 2,402
  • That's Largo in my avatar, not me.
    • View Profile
    • WWW
Re: large csv file to mysql using php?
« Reply #5 on: November 16, 2008, 10:52:42 AM »
If you use PhpMyAdmin, you can do this in 'Import' tab, where you can upload file to server. Otherwise you must upload file using ftp.

If the file is large, you might have to split it into several smaller, because there probably is a limit set up on your host for both upload file size and script execution time.
Logged

PHP 5.2.6 | MySQL 5.1.30
Input queue: sqlite, Zend Framework, PostgreSQL
NetBeans fanatic | ExtJS masochist
I'm true neutral. I go both ways.

paulmo

  • Enthusiast
  • Offline Offline
  • Posts: 74
    • View Profile
Re: large csv file to mysql using php?
« Reply #6 on: November 16, 2008, 08:27:51 PM »
yes there is a limit; 2,000 kbs in import. the files are 99k and 11k. how to split up csv files. thanks
Logged

corbin

  • Guru
  • Fanatic
  • *
  • Offline Offline
  • Posts: 4,084
    • View Profile
Re: large csv file to mysql using php?
« Reply #7 on: November 16, 2008, 10:18:16 PM »
You could just grant an account with your IP or a wild card host access.  Then, you could use the mysql shell tool (mysql or mysql.exe) to connect to it.  Then you could issue the LOAD DATA command from the shell, and it could use a local file.
Logged

Why doesn't anyone ever say hi, hey, or whad up world?
Now I'll be uncreative, and put stats no one cares about.
Win Vista (It doesn't suck!) | Apache 2.2 | PHP 5.2 | MySQL 5 | MSSQL 2008
Fedora Core 9 | Apache 2.2 | PHP 5.2 | MySQL 5
After all, why would you insert your penis into a hole for no reason whatsoever?

Mchl

  • Supporter
  • Addict
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 2,402
  • That's Largo in my avatar, not me.
    • View Profile
    • WWW
Re: large csv file to mysql using php?
« Reply #8 on: November 17, 2008, 02:49:18 AM »
Alternatively, if you have access to PhpMyAdmin's config.inc.php file, you can set up an upload directory in $cfg['UploadDir']
Then upload your files to this directory using ftp. You will then be able to select them within your import dialog.
Logged

PHP 5.2.6 | MySQL 5.1.30
Input queue: sqlite, Zend Framework, PostgreSQL
NetBeans fanatic | ExtJS masochist
I'm true neutral. I go both ways.

paulmo

  • Enthusiast
  • Offline Offline
  • Posts: 74
    • View Profile
Re: large csv file to mysql using php?
« Reply #9 on: November 17, 2008, 07:16:55 AM »
thanks mchl and corbin: those sound like good things to learn. i don't know the first thing about granting an ip acc't or wild card access. i'm guessing this method involves queries in the sql window, which i know little about unless i see the code. the second option sounds good to learn too: i'm unsure about the config file or how to find that in phpmyadmin, and the steps to set up a directory.

thanks for advice. i'm new at this besides creating one table that gets values (insert into) from a form. 
Logged

corbin

  • Guru
  • Fanatic
  • *
  • Offline Offline
  • Posts: 4,084
    • View Profile
Re: large csv file to mysql using php?
« Reply #10 on: November 17, 2008, 05:22:23 PM »
You could use PHPMyAdmin to set the permissions.
Logged

Why doesn't anyone ever say hi, hey, or whad up world?
Now I'll be uncreative, and put stats no one cares about.
Win Vista (It doesn't suck!) | Apache 2.2 | PHP 5.2 | MySQL 5 | MSSQL 2008
Fedora Core 9 | Apache 2.2 | PHP 5.2 | MySQL 5
After all, why would you insert your penis into a hole for no reason whatsoever?
Pages: [1]
 

Page created in 0.06 seconds with 19 queries.