通过PHP将CSV导出到MySQL

时间:2015-10-12 04:20:17

标签: php mysql csv

以下是我的PHP代码:

 JLabel[] label;
    GridLayout experimentLayout = new GridLayout(0, 2);
    panel.setLayout(experimentLayout);
    label = new JLabel[100];
    label[0] = new JLabel("<html><body>\n"
            + "<table width=\"45\" border=\"1\">\n"
            + " <tr>\n"
            + "   <td height=\"63\">Name</td>\n"
            + " </tr>\n"
            + " <tr>\n"
            + "   <td height=\"63\"><div style=\"visibility: hidden\">id</div></td>\n"
            + " </tr>\n"
            + "</table>"
            + "</body></html>");
    panel.add(label[0]);
    panel.revalidate();
    panel.repaint();

我有大约10 MB的CSV文件,我在本地XAMP服务器上通过PHP将其上传到MySQL,但大约需要10到15分钟。我有这个应用程序将上线,因此每次使用很难等待10分钟将CSV传输到MySQL。目前,我使用MySQL-Front,只需1秒钟。请告诉我如何通过PHP加速CSV到MySQL。

1 个答案:

答案 0 :(得分:0)

if(isset($_POST["Import"]))
{

$host='localhost'; // Host Name.

$db_user= '****'; //User Name

$db_password= '****';

$db= '*****'; // Database Name.

$conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());

$csv = $_FILES["file"]["tmp_name"];

if($_FILES["file"]["size"] > 0)

{    
$csvfile = fopen($csv, 'r');

$field_csv = array();

$i = 0;

while (($csv_data = fgetcsv($csvfile, 1024, ",")) !== FALSE)
{

    if($i==0) { $i++; continue; }  // to exclude first line in the csv file.

    $field_csv['list_catagory_id'] = $csv_data[0];  // first csv field
    $field_csv['list_name'] = $csv_data[1]; // second csv field
    $field_csv['list_tamil'] = $csv_data[2]; // third csv field
    $field_csv['list_hindi'] = $csv_data[3]; // third csv field
    $field_csv['list_botanical'] = $csv_data[4]; 
    $field_csv['list_image'] = $csv_data[5]; 
    $field_csv['list_cook_time'] = $csv_data[6]; 
    $field_csv['list_summary'] = $csv_data[7]; 
    $field_csv['list_ingredients'] = $csv_data[8]; 
    $field_csv['list_instruction'] = $csv_data[9]; 

    $query = "INSERT INTO list SET list_catagory_id = '".$field_csv['list_catagory_id']."',list_name = '".$field_csv['list_name']."',list_tamil = '".$field_csv['list_tamil']."',list_hindi = '".$field_csv['list_hindi']."',list_botanical = '".$field_csv['list_botanical']."',list_image = '".$field_csv['list_image']."',list_cook_time = '".$field_csv['list_cook_time']."',list_summary = '".$field_csv['list_summary']."',list_ingredients = '".$field_csv['list_ingredients']."',list_instruction = '".$field_csv['list_instruction']."' ";
    mysql_query($query);    
}

fclose($csvfile);

}

echo "Data Successfully Imported To Table!!";

}
}