可能重复:
将 CSV 导入 mysql
是的,我需要一些帮助:
我正在尝试使用 php 将 .csv 文件导入 mysql 数据库,而不是通过 phpmyadmin 手动执行。
这是我目前的代码:
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);
}
}
但是我收到以下错误:
Fatal error: Call to undefined method stdClass::execute() in /homepages/19/d372249701/htdocs/business-sites/bowlplex-doubles-new/admin/scores.php on line 44
作为参考,我使用的代码取自:这里
我不熟悉 $db->conn 业务,我习惯于 mysql_connect!所以任何帮助将不胜感激。