【问题标题】:Can't insert 9k words to mysql database无法将 9k 字插入 mysql 数据库
【发布时间】:2015-11-06 12:00:56
【问题描述】:

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

这是我的代码:

//$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 个值的数组。

更新:我添加了以下几行: ini_set('memory_limit', '-1'); ini_set('max_execution_time', '-1'); 在我的mysql中添加: max_allowed_pa​​cket = 32M;

它解决了我的脚本问题!

【问题讨论】:

  • 列数据类型是什么?
  • 我怀疑您超出了列的数据类型所允许的长度。
  • 如果是TEXT,则使用LONGTEXT
  • $add 和 $updated 是否增加。您可能会达到 PHP 内存限制,将文件放入数组而不进行任何查询。检查您的错误日志。
  • 我假设您没有收到任何错误消息 - 您不会错过提及它们 ;-) 您是否检查过脚本实际上进入了该 foreach 循环?例如。通过打印echo 'processing ', count($arr), ' items'; foreach ($arr as ...

标签: php mysql


【解决方案1】:

可能的内存或执行时间限制问题。 尝试在您的 php 文件顶部添加以下 2 行

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

【讨论】:

  • 没有。在我的循环中只有 1779 个回声,并且在脚本不起作用之后没有回声
  • 我可以在循环中添加 1500 次。一切正常。但没有更多的 2 000
  • 您的脚本即将终止。您是否在我的帖子中添加了 2 行并尝试再次运行脚本?
  • 您是在浏览器中运行它还是从 cli 中运行它?可能是页面加载超时 - 您可能需要刷新输出以防止浏览器超时。
  • error_reporting(E_ALL); ini_set('display_errors', 1); ini_set('memory_limit', '-1'); ini_set('max_execution_time', '-1');
【解决方案2】:

上面关于 memory_limitma​​x_execution_time 的帖子应该可以解决您的问题。要捕获任何 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>";
}

【讨论】:

  • 非常感谢。打算试试这个。但即使有内存限制,我的代码也不起作用
  • 是的,代码或多或少都很好,这将为您节省对每条记录的数据库调用,并且可能会运行得更快一些
  • 我将此代码添加到我的 php 文件中。并在浏览器中出现 500 错误。现在我不能运行这个文件,即使是空的。会是什么?
  • 更新语句后缺少括号。请再试一次
  • 现在出现错误。但脚本不起作用。没有动作。刚刚结束
猜你喜欢
  • 1970-01-01
  • 2015-07-22
  • 1970-01-01
  • 2018-10-14
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 2018-05-29
  • 1970-01-01
相关资源
最近更新 更多