這裏是我寫的一些代碼。它處理一個CSV文件並將單獨的行存儲到數據庫表(不同之處在於您有一個TXT文件)。它以250行的批量執行mysql插入。希望它能幫助你!
// read all input rows into an array
echo "Processing input..<br /><br />";
$row = 0;
$input = array();
if (($handle = fopen($file['tmp_name'], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$input[$row][] = addslashes($data[$c]);
}
$row++;
}
fclose($handle);
}
$count = 0;
$q = "INSERT INTO `inputs` (`keyword`, `percent`, `link`, `added_on`) VALUES ";
foreach ($input as $inp) {
$q .= "('" . addslashes($inp[0]) . "', '" . addslashes($inp[1]) . "', '" . addslashes($inp[2]) . "', '" . date('Y-m-d H:i:s') . "'), ";
$count++;
if ($count >= 250) {
$q = substr($q, 0, -2);
$q = mysqli_query($con, $q);
$q = "INSERT INTO `inputs` (`keyword`, `percent`, `link`, `added_on`) VALUES ";
$count = 0;
}
}
if ($count > 0) {
$q = substr($q, 0, -2);
$q = mysqli_query($con, $q);
}
echo "Successfully added " . count($input) . " rows to the input list.";
你想要數據庫中的不同行或一行嗎? – 2014-10-11 08:12:53
我想要數據庫中的不同行。 – NadirDev 2014-10-11 08:14:36
你可以var_dump $ poep並顯示來自test2.txt的一些示例數據嗎?它看起來像我的喜歡你的代碼應該做的伎倆,假設文件的內容是空間分隔 – 2014-10-11 08:18:09