简体   繁体   中英

Import Excel file to MySQL

I am going crazy with one problem in my web app.

I have a SCADA system that everyday dumps excel files to my server with data from one thermal plant.

I have a web app in PHP / Jquery that reads this data from a MySQL database and show this in charts and tables.

But I have to manually import the excel files to MySQL. What I want to do, is to build a PHP script, that will do as follow:

The user tries to make a chart, and if the data is not on MySQL it will ask if he wants to import the data from the excel files in the server. Than the excel files are copied into MySQL temporary tables and inserted into the correspond table. Then the data can be displayed to the user and will keep stored in the MySQL database.

All its working, but I need make an automatic way to import this data from the excel files to MySQL tables.

Thanks in advance for your help

hey there actually i had the same problem before i did the following to solve it: create a function in php to execute query to import the file from your destination public function InsertNewData($filename){ $dbtable = substr($filename, 0,-4);

    $sql = " LOAD DATA LOCAL INFILE 'http://your-ip-and-root/uploads/$filename'"
         . " INTO TABLE $dbtable FIELDS TERMINATED BY ';' LINES TERMINATED BY '\\n'";
    $error = " <font color = 'red'>error Load Data the file Please contact the DB Admin </font>";
    $connection = ResourceManager::getConnection();
    $resultado = 0;
    $connection->beginTransaction();
    try {
        $prepare = $connection->prepare($sql);                     
        $prepare->execute();    
        $connection->commit();
    } catch (Exception $e) {
       echo $error;          
        $connection->rollBack();

    }
    $connection = null;

} and then you can call this function from your page when upload excel file is completed

hope it helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM