"); $sql_handle = mysql_pconnect($sql_server, $sql_username, $sql_password); printf("Selecting...\n
"); if (mysql_select_db($sql_database, $sql_handle) == FALSE) { error_log("Unable to select $sql_database"); print("Unable to select database.\n"); exit(); } $sql_query = "DROP TABLE $sql_tablename"; mysql_query($sql_query); $sql_query = "CREATE TABLE " . $sql_tablename . "(" . "fid mediumint(9) NOT NULL default '0'," . "type text NOT NULL," . "length int(11) NOT NULL default '0'," . "title text NOT NULL," . "options text NOT NULL," . "pickn int(11) NOT NULL default '0'," . "pickpercent int(11) NOT NULL default '0'," . "artist text NOT NULL," . "bitrate text NOT NULL," . "codec text NOT NULL," . "ctime date NOT NULL default '0000-00-00'," . "mtime date NOT NULL default '0000-00-00'," . "last_played date NOT NULL default '0000-00-00'," . "drive tinyint(4) NOT NULL default '0'," . "duration int(11) NOT NULL default '0'," . "file_id text NOT NULL," . "genre text NOT NULL," . "offset smallint(6) NOT NULL default '0'," . "play_count smallint(6) NOT NULL default '0'," . "play_last date NOT NULL default '0000-00-00'," . "samplerate mediumint(9) NOT NULL default '0'," . "skip_count smallint(6) NOT NULL default '0'," . "skipped_count smallint(6) NOT NULL default '0'," . "source text NOT NULL," . "tracknr smallint(6) NOT NULL default '0'," . "year smallint(6) NOT NULL default '0'," . "wendy text NOT NULL," . "comment text NOT NULL," . "date text NOT NULL," . "rid text NOT NULL," . "gfid text NOT NULL," . "import_location text NOT NULL," . "hash text NOT NULL," . "fid_generation text NOT NULL," . "trailer text NOT NULL," . "pin text NOT NULL," . "marked text NOT NULL," . "refs text NOT NULL," . "contents text NOT NULL," . "PRIMARY KEY (fid)," . "UNIQUE KEY fid_2 (fid)," . "KEY fid (fid)" . ") TYPE=MyISAM;"; mysql_query($sql_query); if (mysql_error()) { error_log("Unable to create table $sql_tablename"); print("Unable to create database.\n" . mysql_error()); exit(); } $csv_file = file("$csv_filename"); printf("Importing...\n

"); printf("# Entries = %d

\n", sizeof($csv_file) - 1); $valid = 0; $invalid = 0; $columns = ereg_replace("\"", "", $csv_file[0]); for ($i = 1; $i < sizeof($csv_file); $i++) { // Very ugly this bit to escape the "s but I can't be bothered to clean it up and it does work $escaped = ereg_replace("([^,\\\"])\"([^,\"\n\r])", "\\1\\\"\\2", $csv_file[$i]); $escaped = ereg_replace("([^,\\\"])\"([^,\"\n\r])", "\\1\\\"\\2", $escaped); $escaped = ereg_replace("\"\"([^,\n\r])", "\"\\\"\\1", $escaped); $escaped = ereg_replace("([^,])\"\"", "\\1\\\"\"", $escaped); $sql_query = "INSERT INTO $sql_tablename ($columns) VALUES ($escaped)"; mysql_query($sql_query); if (mysql_error()) { echo "Unable to insert record into database.
$csv_file[$i]
" . mysql_error() . "
$escaped

\n"; $invalid++; } else { $valid++; } } echo "Valid: " . $valid . " Invalid: " . $invalid; mysql_close($sql_handle); ?>