2012-01-11 122 views
1

我正在開發一個網站,允許用戶從他們的計算機上瀏覽和選擇.csv文件。
網站將讀取.csv文件(用逗號分隔的項目)並將所有項目存儲到列表或數組中?之後,它將存儲到我的數據庫中。讀取CSV文件並存儲到MySQL數據庫

請問我該怎麼做? 代碼片段和參考將非常感謝。

在此先感謝!

回答

2

希望您使用PHP開發您的網站,並希望將用戶CSV文件導入到您的MySQL服務器數據庫中。

您可以使用下面的PHP類導入數據: http://www.legend.ws/blog/tips-tricks/csv-php-mysql-import/

假設您已經創建所需的數據庫表。

或者,您可以使用MySQLImport實用程序將任何CSV文件導入到您的MySQL數據庫中。請參閱http://dev.mysql.com/doc/refman/5.0/en/mysqlimport.html URL以獲取有關mySQLImport命令的更多詳細信息。

0

這將上傳CSV文件並將數據導入指定的表格。

//Import the contents of a CSV file after uploading it 
//http://www.bin-co.com/php/scripts/csv_import_export/ 
//Aruguments : $table - The name of the table the data must be imported to 
//    $fields - An array of fields that will be used 
//    $csv_fieldname - The name of the CSV file field 
function CSVImport($table, $fields, $csv_fieldname='csv') { 
    if(!$_FILES[$csv_fieldname]['name']) return; 

    $handle = fopen($_FILES[$csv_fieldname]['tmp_name'],'r'); 
    if(!$handle) die('Cannot open uploaded file.'); 

    $row_count = 0; 
    $sql_query = "INSERT INTO $table(". implode(',',$fields) .") VALUES("; 

    $rows = array(); 

    //Read the file as csv 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     $row_count++; 
     foreach($data as $key=>$value) { 
      $data[$key] = "'" . addslashes($value) . "'"; 
     } 
     $rows[] = implode(",",$data); 
    } 
    $sql_query .= implode("),(", $rows); 
    $sql_query .= ")"; 
    fclose($handle); 

    if(count($rows)) { //If some recores were found, 
     //Replace these line with what is appropriate for your DB abstraction layer 
     mysql_query("TRUNCATE TABLE $table") or die("MySQL Error: " . mysql_error()); //Delete the existing records 
     mysql_query($sql_query) or die("MySQL Error: " . mysql_error()); // and insert the new ones. 

     print 'Successfully imported '.$row_count.' record(s)'; 
    } else { 
     print 'Cannot import data - no records found.'; 
    } 
} 

有關詳細信息,請參考以下鏈接:http://www.bin-co.com/php/scripts/csv_import_export/

0

CSV。從MySQL到'csv'。從'csv'到MySQL 此腳本將表數據轉換爲'csv'文件。您可以在另一臺計算機中從'csv'文件 重新創建表格。

<?php 
// connect to database 
require_once '../db_config.php'; 
$conn = new mysqli ($dbhost, $dbuser, $dbpass, 'olimrefdb'); 
if ($conn->connect_errno) die ('no db connection'); 

$tableToConvert = 'mdata'; 
$csvFileName = $tableToConvert . '_' . date ("Ymd");//will contain 'csv' data 

// -----------------------------MySQL table to CSV-------------------------- 
$tableSchema = $conn->query ("SHOW CREATE TABLE $tableToConvert")->fetch_assoc(); 
$result = $conn->query ("SELECT * FROM $tableToConvert"); 

$handle = fopen ($csvFileName.'.csv', 'w');//to store 'csv' data file 
$handle_schema = fopen ($csvFileName.'_schema.csv', 'w');//to store table schema 

fputcsv ($handle_schema, $tableSchema); 
while ($row = $result->fetch_assoc()) { 
    fputcsv ($handle, $row); 
} 
fclose ($handle); 
fclose ($handle_schema); 

// -----------------------------CSV to MySQL table-------------------------- 

$handle = fopen ($csvFileName.'.csv', 'r');//open 'csv' data file 
$handle_schema = fopen ($csvFileName.'_schema.csv', 'r');//open table schema 

$tableSchema = fgetcsv ($handle_schema); 
$newTableName = "$tableSchema[0]_" . date ("Ymd"); 

// change table name in schema 
$tableSchema [1] = preg_replace ("/$tableSchema[0]/", $newTableName, $tableSchema [1], 1); 

$conn->query ("DROP TABLE IF EXISTS $newTableName"); 
$conn->query ($tableSchema [1]);//CREATE TABLE 
// $conn->query ("CREATE TABLE $newTableName LIKE $tableToConvert");//the same, but without using schema 

while ($row = fgetcsv ($handle)) { 
    $data = implode ("','", $row); //convert array to string 

    if ($conn->query ("INSERT INTO $newTableName values('$data')")) { 
    } else { 
     echo '<span style="color:red">Problem:</span> ' . $data . '<br>'; 
     var_dump ($conn->error_list); 
     fclose ($handle); 
     exit(); 
    } 
} 

fclose ($handle); 
echo 'Congratulation!!!' . '<br>'; 
相關問題