无法使用PHPExcel写入mysql数据库

时间:2013-05-06 07:36:44

标签: php mysql phpexcel

我有一个从WebQuery获取数据的Excel文件,我想读取数据,插入到mySQL数据库中。无法使用以下代码写入mysql数据库。有什么想法吗?

<?php
$db_host = "localhost";$db_user = "root";$db_pass = "";$db_name ="report";$db_table = "test";
require '/PHPExcel/Classes/PHPExcel.php';
require_once '/PHPExcel/Classes/PHPExcel/IOFactory.php';
$objPHPExcel = PHPExcel_IOFactory::load('Refreshables.xlsx');
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) 
{
$worksheetTitle     = $worksheet->getTitle();
$highestRow         = $worksheet->getHighestRow(); // e.g. 10
$highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
    $link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
    for ($row = 2; $row <= $highestRow; ++ $row) 
{
    $val=array();
    for ($col = 0; $col < $highestColumnIndex; ++ $col) 
    {
        $cell = $worksheet->getCellByColumnAndRow($col, $row);
        $val[] = $cell->getValue();
    }
        $sql = "insert into 'test' (AppName, Device_Full_Name, App_Type, Carrier, Language, Version, Status, Platform, Project_Type, Base_Device, Build_Type, Project_ID) values('".$val[0]."', '".$val[1]."', '".$val[2]."','".$val[3]."', '".$val[4]."', '".$val[5]."','".$val[6]."', '".$val[7]."', '".$val[8]."','".$val[9]."', '".$val[10]."', '".$val[11]."')";
        mysql_query($sql);
} 
}
mysql_close($link);
?>

1 个答案:

答案 0 :(得分:0)

更改

$sql = "insert into 'test' (AppName,...
                    ^    ^ don't use quotes in table or column names

$sql = "insert into `test` (AppName,...

甚至没有后退嘀嗒

$sql = "insert into test (AppName,...