I'm trying to grab data from an XLS file and dump it into an mySQL DB.
I located a php file that will take the contents of the XLS and display them - as tabular data - on a web page. I have attempted to modify this code to insert my data into a DB.
Here is the function that does the work - and that I have been attempting to modify:
function dumpSQL($row_numbers=false,$col_letters=false,$sheet=0) {
$xlsData = '';
for($row=1;$row<=$this->rowcount($sheet);$row++) {
$rowheight = $this->rowheight($row,$sheet);
for($col=1;$col<=$this->colcount($sheet);$col++) {
if(!$this->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint']) {
// cell value
//$val = $this->val($row,$col,$sheet);
$xlsData[$row][$col] = $this->val($row,$col,$sheet);
}
}
}
// Make a MySQL Connection
mysql_connect("*******", "******", "*******") or die(mysql_error());
mysql_select_db("***********") or die(mysql_error());
$sql = "INSERT INTO BEER_LOCATIONS (LOCATION_NAME, LOCATION_STREET, LOCATION_CITY, LOCATION_STATE, LOCATION_ZIPCODE, LOCATION_PHONE, BEER_STYLE ) VALUES ($xlsData)";
$result = mysql_query("
$sql
") or die(mysql_error());
}
I get the following error when I run this code: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
I have also tried to remove the portion where it obtains the multidimensional array and just build a static version:
$xlsData[0][0] = "Hello World";
$xlsData[0][1] = "38380 - Thirteen Coins-Boren";
$xlsData[0][2] = "125 Boren Ave N";
$xlsData[0][3] = "Seattle";
$xlsData[0][4] = "WA";
$xlsData[0][5] = "98109";
$xlsData[0][6] = "2066822513";
$xlsData[0][7] = "34740 - Georgetown Keg - 1/2 Manny Pale";
$xlsData[1][0] = "Hello World";
$xlsData[1][1] = "38380 - Thirteen Coins-Boren";
$xlsData[1][2] = "125 Boren Ave N";
$xlsData[1][3] = "Seattle";
$xlsData[1][4] = "WA";
$xlsData[1][5] = "98109";
$xlsData[1][6] = "2066822513";
$xlsData[1][7] = "34740 - Georgetown Keg - 1/2 Manny Pale";
$xlsData[2][0] = "Hello World";
$xlsData[2][1] = "38380 - Thirteen Coins-Boren";
$xlsData[2][2] = "125 Boren Ave N";
$xlsData[2][3] = "Seattle";
$xlsData[2][4] = "WA";
$xlsData[2][5] = "98109";
$xlsData[2][6] = "2066822513";
$xlsData[2][7] = "WA";
$xlsData[3][0] = "Hello World";
$xlsData[3][1] = "86036 - Aafes Ft Lewis Class Vi";
$xlsData[3][2] = "Bldg 2202";
$xlsData[3][3] = "Fort Lewis";
$xlsData[3][4] = "WA";
$xlsData[3][5] = "98433";
$xlsData[3][6] = "2539644128";
$xlsData[3][7] = "42624 - Georgetown Keg - 1/4 Manny Pale";
I end up with the same error.
I attempted to serialize my data prior to inserting it into my DB:
$xlsData = serialize($xlsData);
Thanks for any help.
I'm having trouble implementing the mysql_real_escape_string(). It seems like it needs to wrap around the implode(), but I get an error when I try that.
$sql = "INSERT INTO BEER_LOCATIONS (LOCATION_NAME, LOCATION_STREET, LOCATION_CITY, LOCATION_STATE, LOCATION_ZIPCODE, LOCATION_PHONE, BEER_STYLE ) VALUES ('" . mysql_real_escape_string(implode("', '", $row)) . "')";
I get this error when I try that: Column count doesn't match value count at row 1
Thanks again.