本文介绍了将CSV文件直接导入MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 右键我需要一些帮助: 我试图使用php将.csv文件导入mysql数据库,而不是通过phpmyadmin手动导入。 这是我现在的代码: if($ _ REQUEST ['func'] == iid){ $ db-> conn = new mysqli(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME)或 die('连接到数据库时出现问题。 $ csv = $ _POST ['csv-file']; $ path = $ csv; $ row = 1; if(($ handle = fopen($ path,r))!== FALSE){ while(($ data = fgetcsv($ handle,1000,;))! = FALSE){ $ row ++; $ data_entries [] = $ data; } fclose($ handle); } //你必须扩展 foreach($ data_entries as $ line){ $ sql = $ db-> conn-> prepare INSERT INTO`bd_results`'); $ db-> execute($ line);但是,我得到以下错误: 致命错误:调用未定义的方法stdClass :: execute()in / homeepages / 19 / d372249701 / htdocs / business-sites / bowlplex-doubles -new / admin / scores.php on line 44 参考我使用的代码取自: 此处 我不是很精通在$ db-> conn业务我习惯了mysql_connect! 解决方案尝试这个简单的方法。 if(($ handle = fopen(google.csv,r))!== FALSE){ while(($ data = fgetcsv ,1000,))!== FALSE){ $ db-> conn-> query(INSERT INTO values('。implode('\',\'数据)');); } fclose($ handle); } 您的脚本也可能需要为CSV值添加引号, t已经有引号。如果您需要处理CSV文件中的引号和所有内容,我们建议您查看我的博文: http://www.fusionswift.com/2012/07/php-import-csv-to-mysql/ Right I need some help with this:I am trying to import a .csv file into a mysql database using php, rather than doing it manually through phpmyadmin.This is the code I have at the moment:if($_REQUEST['func'] == "iid"){ $db->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database.'); $csv = $_POST['csv-file']; $path = $csv; $row = 1; if (($handle = fopen($path, "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { $row++; $data_entries[] = $data ; } fclose($handle); } // this you'll have to expand foreach($data_entries as $line){ $sql = $db->conn->prepare('INSERT INTO `bd_results`'); $db->execute($line); }}However I get the following error:Fatal error: Call to undefined method stdClass::execute() in /homepages/19/d372249701/htdocs/business-sites/bowlplex-doubles-new/admin/scores.php on line 44For reference I am using this code taken from: HereI am not well versed in the $db->conn business I'm used to mysql_connect!! so any help would be appreciated. 解决方案 Try this simple one.if (($handle = fopen("google.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $db->conn->query("INSERT INTO values('" . implode('\',\'', $data) . "');"); } fclose($handle);}Your script might also need to add quotes to the CSV values if they don't have quotes already. If you'll be needing to deal with quotes and all in your CSV files, I recommend you look at my blog post at http://www.fusionswift.com/2012/07/php-import-csv-to-mysql/ 这篇关于将CSV文件直接导入MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-11 02:38