Skip to content

Instantly share code, notes, and snippets.

@arschmitz
Created November 17, 2010 15:24
Show Gist options
  • Save arschmitz/703502 to your computer and use it in GitHub Desktop.
Save arschmitz/703502 to your computer and use it in GitHub Desktop.
<?php
$databasehost = "localhost";
$databasename = "bdaginventory";
$databasetable = "inventorytest";
$databaseusername ="bdag";
$databasepassword = "bdag";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "vehicles.csv";
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();
foreach(explode($lineseparator,$csvcontent) as $line) {
$lines++;
$line = trim($line," \t");
$line = str_replace("\r","",$line);
/************************************
This line escapes the special character.
************************************/
$line = str_replace("'","\'",$line);
$line = str_replace('"','',$line);
/*************************************/
$linearray = explode($fieldseparator,$line);
echo count($linearray)."\n";
array_splice($linearray, 88);
echo count($linearray)."\n";
$linemysql = implode("', '",$linearray);
$linemysql .= ",";
//echo $linemysql;
$query = "insert into $databasetable values('$linemysql');";
$queries .= $query . "\n";
echo $query."\n";
@mysql_query($query);
}
@mysql_close($con);
echo "Found a total of $lines records in this csv file.\n";
?>
@arschmitz
Copy link
Author

insert into inventorytest values('Vehicle_ID', 'Dealer_ID', 'location', 'type', 'stock', 'vin', 'year', 'make', 'model', 'model_number', 'trim', 'Style_Description', 'body', 'doors', 'ext_color', 'ext_color_generic', 'Ext_Color_Code', 'int_color', 'int_color_generic', 'Int_Color_Code', 'Int_Upholstery', 'miles', 'engine_cylinders', 'engine_displacement', 'engine_block_type', 'engine_aspiration_type', 'Engine_Description', 'transmission', 'Transmission_Speed', 'Transmission_Description', 'Drivetrain', 'Fuel_Type', 'EPA_City', 'EPA_Highway', 'Wheelbase_Code', 'certified', 'selling_price', 'msrp', 'book_value', 'invoice', 'nada_price', 'internet_price', 'misc_price1', 'misc_price2', 'misc_price3', 'date_in_stock', 'Days_In_Stock', 'comment1', 'comment2', 'comment3', 'comment4', 'comment5', 'description', 'options', 'Dealer_Options', 'Vehicle_Is_Sold', 'Vehicle_Is_Locked', 'Date_Created', 'Date_Modified', 'Specials_Price', 'Specials_Start_Date', 'Specials_End_Date', 'Specials_Hide_Regular_Price', 'Specials_Monthly_Payment', 'Specials_Disclaimer', 'Package_Codes', 'Package_Descriptions', 'Factory_Codes', 'Factory_Descriptions', 'Make_Standard', 'Model_Standard', 'Body_Standard', 'Stock_Photo_Name', 'Dealer_Photo_Count', 'Photo_URL', 'Window_Sticker_Published', 'Window_Sticker_Last_Published', 'MSRP', 'Clean_Wholesale', 'Average_Wholesale', 'End_of_Term', 'UVC', 'Equip_Retail_Ind', 'Clean_Retail', 'Average_Retail', 'Regional_Adjustment', 'Clean_Mileage_Adjustment', 'Average_Mileage_Adjustment');

adds 1 row with all values blank!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment