EXCEL文件导入MYSQL出错
在的表格很简单,就12列数据,要导入MYSQL
在网上找的代码,自己测试的文件和下载的文件都在一个文件夹里,
有四个文件1,data.xls 2.index.php 3,reader.php(下载的) 4,oleread.inc(下载的)
data.xls表格里数据
num,names,years,season,groups,colornote,colornum,price,types,sizes,materials,note
有两行记录
index代码如下
<?<br />require_once 'reader.php'; // ExcelFile($filename, $encoding); $data = new Spreadsheet_Excel_Reader(); // Set output Encoding. $data->setOutputEncoding('gbk'); <br />//”data.xls”是指要导入到mysql中的excel文件 <br />$data->read('data.xls'); <br />@ $db = mysql_connect('localhost', 'root', '123123') or <br />die("Could not connect to database.");//连接数据库 <br />mysql_query("set names 'gbk'");//输出中文 <br />mysql_select_db('test'); //选择数据库 <br />error_reporting(E_ALL ^ E_NOTICE); <br />for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {<br />//以下注释的for循环打印excel表数据 <br />/* <br />for ($j = 1; $j < = $data->sheets[0]['numCols']; $j++) { <br />echo "\"".$data->sheets[0]['cells'][$i][$j]."\","; <br />} <br />echo "\n"; <br />*/ <br />//以下代码是将excel表数据【3个字段】插入到mysql中, <br />//根据你的excel表字段的多少,改写以下代码吧! <br />$sql = "INSERT INTO test <br />(<br /> num,names,years,season,groups,colornote,colornum,price,types,sizes,materials,note<br />)<br />VALUES('". <br />$data->sheets[0]['cells'][$i][1]."','". <br />$data->sheets[0]['cells'][$i][2]."','". <br />$data->sheets[0]['cells'][$i][4]."','". <br />$data->sheets[0]['cells'][$i][5]."','". <br />$data->sheets[0]['cells'][$i][6]."','". <br />$data->sheets[0]['cells'][$i][7]."','". <br />$data->sheets[0]['cells'][$i][8]."','". <br />$data->sheets[0]['cells'][$i][9]."','". <br />$data->sheets[0]['cells'][$i][10]."','". <br />$data->sheets[0]['cells'][$i][11]."','". <br />$data->sheets[0]['cells'][$i][12]."')"; <br />echo $sql.'< br />'; <br />$res = mysql_query($sql); <br />} <br />?>
运行index.php报以下错误
Fatal error: Call to a member function read() on a non-object in D:\AppServ\www\aaee\index.php on line 4
------解决方案--------------------
第 4 行 $data->read('data.xls');
这和 $data 是在哪里实例化的?
显然你也太粗心了,从网上复制的代码也不认真整理一下
那句 $data = new Spreadsheet_Excel_Reader(); 不还在第二行的注释中吗?

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools
