在PHP中从csv文件动态获取表名和字段并导入到MYSQL [英] In Php to dynamically get table name and fields from csv file and import to MYSQL

查看:91
本文介绍了在PHP中从csv文件动态获取表名和字段并导入到MYSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mysql中用表名(CSV文件名)和字段名(CSV列名)创建了一个数据结构.

I have created a data-structure in Mysql with table name(CSV filename) and field names(CSV column names).

现在,我正在成功地将数据从csv导入到Mysql表中,就像我在脚本中硬编码csv文件名和字段名一样.如何动态获取,因为我有很多csv文件要导入到mysql中.

Right now I am importing the data from csv to Mysql table successfully Where as I am hard-coding csv file name and field name in script. How to dynamical fetch bec I have manny csv files to import into mysql.

<?php
include "db.php";
$filename = "C:\REQ\Status.csv";
if (($handle = fopen($filename, 'r')) !== FALSE)
     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
     {
       print_r($data);
       $import="INSERT into status(status) values('$data[1]')";
       mysql_query($import) or die(mysql_error());

     }
 fclose($handle);

?>    

推荐答案

我已经实现了此代码,并且它是经过测试的代码.我认为它非常有用

I have implement this code and it is tested code. I think it is very use full

您已遵循一些规则:-

1.根据数据库表名称创建您的csv文件(例如:数据库表名称为users,则csv应为users.csv)

1.your csv file according to database table name (ex: db table name is users then csv should be users.csv)

2.csv文件的第一行应该是开始输入数据后的db表字段名称(例如ID,名称等)

2.Your csv file's first row should be db table fields name (ex: Id, name etc) after the start your data entry

3.您可以从以下位置下载数据源类:- http://code. google.com/p/php-csv-parser/ 因为我需要以下代码:require_once'CSV/DataSource.php';

3.you can download data source class from :- http://code.google.com/p/php-csv-parser/ because i have require below the code: require_once 'CSV/DataSource.php';

<?php
ini_set('memory_limit','512M');
$dbhost = "localhost";
$dbname = "excel_import";
$dbuser = "root";
$dbpass = "";

$conn=mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error());
mysql_select_db($dbname) or die("Unable to select database because: " . mysql_error());


require_once 'CSV/DataSource.php';


$filename = "users.csv";
$ext = explode(".",$filename);
$path = "uploads/".$filename;

$dbtable = $ext[0];

import_csv($dbtable, $path);


function import_csv($dbtable, $csv_file_name_with_path)
{
    $csv = new File_CSV_DataSource;
    $csv->load($csv_file_name_with_path);

    $csvData = $csv->connect();

    $res='';
    foreach($csvData  as $key)
    {
        $myKey ='';
        $myVal='';
        foreach($key as $k=>$v)
        {
            $myKey .=$k.',';
            $myVal .="'".$v."',";
          }

        $myKey = substr($myKey, 0, -1);
        $myVal = substr($myVal, 0, -1); 
        $query="insert into ".$dbtable." ($myKey)values($myVal)";
        $res=  mysql_query($query);

    }

    if($res ==1)
    {

                echo "record successfully Import.";

    }else{

                echo "record not successfully Import.";
    }
}

这篇关于在PHP中从csv文件动态获取表名和字段并导入到MYSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆