PHP多维数组到mySQL数据库

时间:2011-04-25 19:51:46

标签: php mysql serialization multidimensional-array

我正在尝试从XLS文件中获取数据并将其转储到mySQL数据库中。

我找到了一个php文件,它将获取XLS的内容并在网页上显示它们 - 作为表格数据。我试图修改此代码以将数据插入数据库。

这是完成工作的功能 - 我一直在尝试修改:

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

运行此代码时出现以下错误: 您的SQL语法有错误;查看与MySQL服务器版本对应的手册,以获得正确的语法

我还尝试删除获取多维数组的部分,然后构建一个静态版本:

$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";

我最终遇到了同样的错误。

我尝试在将数据插入数据库之前序列化我的数据:

$xlsData = serialize($xlsData);

感谢您的帮助。

我在实现mysql_real_escape_string()时遇到了麻烦。看起来它需要包围implode(),但是当我尝试时我得到一个错误。

$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)) . "')"; 

我尝试时遇到此错误: 列数与第1行的值计数不匹配

再次感谢。

2 个答案:

答案 0 :(得分:0)

尝试替换它:

$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());

用这个:

foreach($xlsData as $row) {
    $sql = "INSERT INTO BEER_LOCATIONS (LOCATION_NAME, LOCATION_STREET, LOCATION_CITY, LOCATION_STATE, LOCATION_ZIPCODE, LOCATION_PHONE, BEER_STYLE ) VALUES ('" . implode("', '", $row) . "')";
    $result = mysql_query($sql) or die(mysql_error());
}

你必须一次插入一行,这就是foreach循环完成的内容。此外,您不能只为数组放置数组本身。 implode()函数将列值数组转换为字符串。

编辑:此外,您应该将函数开头附近的$xlsData = '';更改为$xlsData = array();我实际上并不确定您的方式是否有问题,但由于$ xlsData应该是一个数组,最好将它设置为空数组而不是空字符串。

答案 1 :(得分:0)

我从其他有帮助的人那里得到了答案。感谢所有帮助过我的人:)

implode("', '", array_map('mysql_real_escape_string', $row))