2015-11-06 21 views
2

我正在将数据txt文件插入到我的MySQL数据库中。有100-400字的文件插入太棒了。他们没问题。但是今天我得到了一个带有9k字的文件,我的脚本停止了工作。它插入或更新什么。我的脚本有问题吗,还是应该制作这个文件的一部分?什么是更好的解决方案?无法插入9k字到mysql数据库

这里是我的代码:

//$arr - array from file; 

foreach ($arr as $temp) 
{ 
    $w_name = mysqli_real_escape_string($db_connect, $temp[0]); 
    $w_minprice = $temp[2]; 
    $w_js_id = $temp[1]; 
    $w_s = $temp[3]; 

    $sql ="SELECT `js_id` FROM `mytable` WHERE `js_id` = '$w_js_id' LIMIT 1"; 
    $result = mysqli_query($db_connect, $sql) or die(mysql_error()); 

    list($temp2) = mysqli_fetch_row($result); 

    if ($temp2 == '') 
    { 
     $sql = "INSERT INTO `mytable` (`name`, `minprice`, `s`, `js_id`, `pl`) VALUES ('$w_name', '$w_minprice', '$w_s', '$w_js_id', '$pl')"; 
     $result = mysqli_query($db_connect, $sql) or die(mysql_error()); 
     if ($result) {$added++;} 
    } 
    else 
    { 
     $sql = "UPDATE `mytable` SET `minprice` = '$w_minprice', `s` = '$g_s' WHERE `js_id` = '$w_js_id' LIMIT 1"; 
     $result = mysqli_query($db_connect, $sql) or die(mysql_error()); 
     if ($result) {$updated++;} 
    } 
} 

也许我应该做多个查询?或将我的查询分为10?但是如何?我在MySQL查询方面不够强大。 我的目标是将一个有9 000个值的数组插入到我的数据库中。

UPDATE:I added this lines: ini_set('memory_limit','-1'); ini_set('max_execution_time','-1'); 加入我的mysql: max_allowed_pa​​cket = 32M;

它解决了我的脚本问题!

+0

什么是列中的数据类型? –

+0

我怀疑你超出了列的数据类型所允许的长度。 – Alfie

+0

如果它的'TEXT'然后使用'LONGTEXT' –

回答

1

上述职位有关memory_limit的的max_execution_time应该解决您的问题的顶部添加下面2行。为了赶上任何PHP的问题,您可以添加

error_reporting(E_ALL); 
ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
ini_set('memory_limit', '256M'); 
ini_set('max_execution_time', '240'); 

我只是想提出一个更优化的方式做进口,将节省您的查询,并可能运行得更快

$sthi=mysqli_prepare($db_connect,"INSERT INTO `mytable` (`name`, `minprice`, `s`, `js_id`, `pl`) VALUES (?, ?, ?, ?, ?)") or die(mysqli_error($db_connect)); 
$sthi->bind_param("sssss", $w_name, $w_minprice,$w_s,$w_js_id,$pl); 
$sthu=mysqli_prepare($db_connect,"UPDATE `mytable` SET `minprice` = ?, `s` = ? WHERE `js_id` = ? LIMIT 1") or die(mysqli_error($db_connect)); 
$sthu->bind_param("sss", $w_minprice,$w_s,$w_js_id); 
$i=0; 
foreach ($arr as $temp) { 
    $w_name = $temp[0]; 
    $w_minprice = $temp[2]; 
    $w_js_id = $temp[1]; 
    $w_s = $temp[3]; 

    @$sthi->execute(); 
    //If insert was unsuccessfull the record already exists 
    if($sthi->affected_rows==1){ 
    $added++; 
    } 
    //Otherwise you need to update it - run the update startement 
    else { 
    if($sthu->execute()){ 
     if($sthu->affected_rows==1){ 
     $updated++; 
     } 
    } 
    else echo $sthu->error."<br>"; 
    } 
    flush(); 
    set_time_limit(30); 

    //Not really necessary - just for debugging 
    echo ++$i." u:".$updated." a:".$added."<br>"; 
} 
+0

非常感谢。去试试这个。但我的代码不起作用,即使内存限制 – mrdeath4

+0

是代码是或多或少罚款,这将节省您在每个记录上的数据库调用,并可能会运行得更快 –

+0

我将此代码添加到我的PHP文件。在浏览器中得到了500个错误。现在我不能运行这个文件甚至空。会是什么呢? – mrdeath4

2

可能的内存或执行时间限制问题。 尝试在你的PHP文件

ini_set('memory_limit', '-1'); 
ini_set('max_execution_time', '-1'); 
+0

no。只是1779回声在我的循环和脚本不工作后没有回应 – mrdeath4

+0

我可以添加我的循环1500次,一切正常,但没有更多2 000 – mrdeath4

+0

您的脚本正在终止。您是否在我的文章中添加了2行并尝试再次运行脚本? – Samir