Excel file import MySQL Error
This post was last edited by xuzuning on 2013-11-14 13:36:39
In the table is very simple, just 12 columns of data, to import MySQL
Find the code on the Internet, your own test files and downloaded files are in a folder,
There are four files 1,data.xls 2.index.php 3,reader.php (downloaded) 4,oleread.inc (downloaded)
Data.xls Data in the table
Num,names,years,season,groups,colornote,colornum,price,types,sizes,materials,note
There are two lines of records
The index code is as follows
Require_once ' reader.php '; Excelfile ($filename, $encoding); $data = new Spreadsheet_excel_reader (); Set output Encoding. $data->setoutputencoding (' GBK ');
"Data.xls" means an Excel file to import into MySQL
$data->read (' Data.xls ');
@ $db = mysql_connect (' localhost ', ' root ', ' 123123 ') or
Die ("Could not connect to database."); /Connection Database
mysql_query ("Set names ' GBK '");//Output Chinese
mysql_select_db (' Test '); Select Database
Error_reporting (e_all ^ e_notice);
for ($i = 1; $i <= $data->sheets[0][' numrows '); $i + +) {
The For loop of the following comments prints the Excel table data
/*
for ($j = 1; $j < = $data->sheets[0][' numcols '); $j + +) {
echo "\" ". $data->sheets[0][' cells ' [$i] [$j]." \",";
}
echo "\ n";
*/
The following code inserts the Excel table data "3 fields" into MySQL,
Depending on how much of your Excel table field, rewrite the following code!
$sql = "INSERT into test
(
Num,names,years,season,groups,colornote,colornum,price,types,sizes,materials,note
)
VALUES (' ".
$data->sheets[0][' cells ' [$i][1]. "', '".
$data->sheets[0][' cells ' [$i][2]. "', '".
$data->sheets[0][' cells ' [$i][4]. "', '".
$data->sheets[0][' cells ' [$i][5]. "', '".
$data->sheets[0][' cells ' [$i][6]. "', '".
$data->sheets[0][' cells ' [$i][7]. "', '".
$data->sheets[0][' cells ' [$i][8]. "', '".
$data->sheets[0][' cells ' [$i][9]. "', '".
$data->sheets[0][' cells ' [$i][10]. "', '".
$data->sheets[0][' cells ' [$i][11]. "', '".
$data->sheets[0][' cells '] [$i][12]. "')";
echo $sql. ' < br/> ';
$res = mysql_query ($sql);
}
?>
Run index.php report the following error
Fatal Error:call to a member function read () on a non-object in D:\AppServ\www\aaee\index.php on line 4
Share to: more
------Solution--------------------
Line 4th $data->read (' Data.xls ');
Where did this and $data be instantiated?
Obviously you are too careless, the code copied from the Internet is not carefully organized
The sentence $data = new Spreadsheet_excel_reader (); Not yet in the comments in the second line?