直接更改mysql数据文件csv,将CSV文件直接导入MySQL数据库

Possible Duplicate:

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 44

For reference I am using this code taken from: Here

I 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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值