我想在我的项目中使用PHPExcel从一台服务器读取xlsx文件,并将数据写入另一台服务器的DB(phpmyadmin)。我不太了解php,我需要有人来帮助我。所以我不知道如何连接到该服务器:( 我的代码在这里有一些错误:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("osweh", $con);
require_once 'PHPExcel.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("Book1.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$rows = array();
for ($row = 2; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
$rows[$col] = mysql_real_escape_string($objWorksheet->getCellByColumnAndRow($col, $row)->getValue());
}
mysql_query("INSERT INTO xlsx ('f1','f2','f3','f4') VALUES ($rows[1],$rows[2],$row[3],$row[4]);");
echo mysql_error;
}
mysql_close($con);
?>
答案 0 :(得分:0)
尝试改变
mysql_query("INSERT INTO xlsx ('f1','f2','f3','f4') VALUES ($rows[1],$rows[2],$row[3],$row[4]);");
echo mysql_error;
到
$result = mysql_query("INSERT INTO xlsx ('f1','f2','f3','f4') VALUES ($rows[1],$rows[2],$row[3],$row[4]);");
if ($result === false) {
die('Invalid query: ' . mysql_error());
}
它可以帮助您在插入数据不正确时看到错误
答案 1 :(得分:0)
修改声明:
$result = mysql_query("INSERT INTO `xlsx` (`f1`,`f2`,`f3`,`f4`) " .
" VALUES ( '$rows[1]', '$rows[2]', '$row[3]', '$row[4]');");
if ($result === false) {
die('Invalid query: ' . mysql_error());
}