Php 每天从CSV文件更新MySQL(存储过程中不允许加载数据)

Php 每天从CSV文件更新MySQL(存储过程中不允许加载数据),php,mysql,sql,csv,phpmyadmin,Php,Mysql,Sql,Csv,Phpmyadmin,我在我的网站(php语言)工作,我想每天用csv文件更新我的表格,所以我写了以下内容: LOAD DATA LOCAL INFILE 'C:/wamp/www/mywebsite/data/m5.csv' INTO TABLE m4 FIELDS TERMINATED BY "," LINES TERMINATED BY "\n" IGNORE 1 LINES (col1,col2,col3,col4,col5) 我已经在PhpMyAdmin中创建了一个事件,但是当我不想使我的事件有效时

我在我的网站(php语言)工作,我想每天用csv文件更新我的表格,所以我写了以下内容:

LOAD DATA LOCAL INFILE 'C:/wamp/www/mywebsite/data/m5.csv' 
INTO TABLE m4 
FIELDS TERMINATED BY "," 
LINES TERMINATED BY "\n"
IGNORE 1 LINES
(col1,col2,col3,col4,col5)
我已经在PhpMyAdmin中创建了一个事件,但是当我不想使我的事件有效时,出现了以下错误:

存储过程中不允许加载数据

如何使用CSV文件每天更新表格


谢谢你

关于这个问题:

您的问题的解决方案可能是

<?php
$query = <<<eof
    LOAD DATA INFILE 'C:/wamp/www/mywebsite/data/m5.csv'
    INTO TABLE m4
    FIELDS TERMINATED BY ','
    OPTIONALLY ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (col1,col2,col3,col4,col5)
eof;

$db->query($query);
?>

但是,如果您对执行相同操作的php脚本感兴趣,可以尝试以下方法

<?php
$dbhost =  "";
$dbname = "";
$dbusername = "";
$dbpassword = "";


$dbtable = "m4";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "C:/wamp/www/mywebsite/data/m5.csv";

/********************************/
/* Set this to 1 if your table have an auto_increment field.
/********************************/
$addauto = 0;
/********************************/
/* Would you like to save the mysqli queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************/
$save = 0;
$outputfile = "";


if(!file_exists($csvfile)) {
    echo "File not found. Make sure you specified the correct path.\n";
    exit;
}

$file = fopen($csvfile,"r");

if(!$file) {
    echo "Error opening data file.\n";
    exit;
}

$size = filesize($csvfile);

if(!$size) {
    echo "File is empty.\n";
    exit;
}

$csvcontent = fread($file,$size);

fclose($file);

$conn = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname) or die('Could not connect');

mysqli_set_charset($conn,"utf8");

$sqldel="DELETE FROM ".$dbtable;

$conn->query($sqldel);
$lines = 0;
$queries = "";
$linearray = array();

foreach(explode($lineseparator,$csvcontent) as $line) {

    $lines++;

    $line = trim($line," \t");

    $line = str_replace("\r","",$line);

    /************************************
    This line escapes the special character. remove it if entries are already escaped in the csv file
    ************************************/
    $line = str_replace("'","\'",$line);
    /*************************************/

    $linearray = explode($fieldseparator,$line);

    $linemysql = implode("','",$linearray);

    $linemysql = utf8_encode($linemysql);

    if($addauto)
        $query = "INSERT INTO $dbtable VALUES('','$linemysql');";
    else
        $query = "INSERT INTO $dbtable VALUES('$linemysql');";

    $queries .= $query . "\n";

    $res_i=$conn->query($query);
}


if($save) {

    if(!is_writable($outputfile)) {
        echo "File is not writable, check permissions.\n";
    }

    else {
        $file2 = fopen($outputfile,"w");

        if(!$file2) {
            echo "Error writing to the output file.\n";
        }
        else {
            fwrite($file2,$queries);
            fclose($file2);
        }
    }

}
?>

关于这个问题:

您的问题的解决方案可能是

<?php
$query = <<<eof
    LOAD DATA INFILE 'C:/wamp/www/mywebsite/data/m5.csv'
    INTO TABLE m4
    FIELDS TERMINATED BY ','
    OPTIONALLY ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (col1,col2,col3,col4,col5)
eof;

$db->query($query);
?>

但是,如果您对执行相同操作的php脚本感兴趣,可以尝试以下方法

<?php
$dbhost =  "";
$dbname = "";
$dbusername = "";
$dbpassword = "";


$dbtable = "m4";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "C:/wamp/www/mywebsite/data/m5.csv";

/********************************/
/* Set this to 1 if your table have an auto_increment field.
/********************************/
$addauto = 0;
/********************************/
/* Would you like to save the mysqli queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************/
$save = 0;
$outputfile = "";


if(!file_exists($csvfile)) {
    echo "File not found. Make sure you specified the correct path.\n";
    exit;
}

$file = fopen($csvfile,"r");

if(!$file) {
    echo "Error opening data file.\n";
    exit;
}

$size = filesize($csvfile);

if(!$size) {
    echo "File is empty.\n";
    exit;
}

$csvcontent = fread($file,$size);

fclose($file);

$conn = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname) or die('Could not connect');

mysqli_set_charset($conn,"utf8");

$sqldel="DELETE FROM ".$dbtable;

$conn->query($sqldel);
$lines = 0;
$queries = "";
$linearray = array();

foreach(explode($lineseparator,$csvcontent) as $line) {

    $lines++;

    $line = trim($line," \t");

    $line = str_replace("\r","",$line);

    /************************************
    This line escapes the special character. remove it if entries are already escaped in the csv file
    ************************************/
    $line = str_replace("'","\'",$line);
    /*************************************/

    $linearray = explode($fieldseparator,$line);

    $linemysql = implode("','",$linearray);

    $linemysql = utf8_encode($linemysql);

    if($addauto)
        $query = "INSERT INTO $dbtable VALUES('','$linemysql');";
    else
        $query = "INSERT INTO $dbtable VALUES('$linemysql');";

    $queries .= $query . "\n";

    $res_i=$conn->query($query);
}


if($save) {

    if(!is_writable($outputfile)) {
        echo "File is not writable, check permissions.\n";
    }

    else {
        $file2 = fopen($outputfile,"w");

        if(!$file2) {
            echo "Error writing to the output file.\n";
        }
        else {
            fwrite($file2,$queries);
            fclose($file2);
        }
    }

}
?>

您的CSV仅包含所有数据库记录或更新的记录?每天我都会截断我的表,并希望使用CSV文件在中插入新信息。您是否希望使用php脚本而不是PhpMyAdmin事件执行此操作?是!但有可能在我的wamp serveur中测试它吗?感谢您您的CSV仅包含所有数据库记录或更新的记录?每天我都会截断我的表,并希望使用CSV文件在中插入新信息您是否有兴趣使用php脚本而不是PhpMyAdmin事件执行此操作?是!但有可能在我的wamp serveur中测试它吗?非常感谢。