【问题标题】:query insert only one row instead multiple rows查询只插入一行而不是多行
【发布时间】:2018-07-01 07:21:32
【问题描述】:

我试图从 excel 工作表中插入多个值,我将所有数据从 excel 工作表中获取到一个数组变量。但是当我插入它时,它总是只插入第一行,我有一种方法可以使用 PDO 执行插入查询,

我的数据

array (size=4)
  1 => 
    array (size=5)
      'A' => string '*' (length=1)
      'B' => string 'Title' (length=5)
      'C' => string 'Author' (length=6)
      'D' => string 'Publication ' (length=12)
      'E' => string 'Container' (length=9)
  2 => 
    array (size=5)
      'A' => float 1
      'B' => string 'Test' (length=4)
      'C' => string 'one' (length=3)
      'D' => string 'two' (length=3)
      'E' => string 'X1' (length=2)
  3 => 
    array (size=5)
      'A' => float 2
      'B' => string 'Test' (length=4)
      'C' => string 'three' (length=5)
      'D' => string 'four' (length=4)
      'E' => string 'X2' (length=2)
  4 => 
    array (size=5)
      'A' => float 3
      'B' => string 'Test' (length=4)
      'C' => string 'five' (length=4)
      'D' => string 'six' (length=3)
      'E' => string 'X3' (length=2)

这是我的方法

public function importBooks($data, $nr)
{
    // Init query
    $this->db->query('INSERT INTO books_pre (title, author, publication, container, created_by, created_at) VALUES (:title, :author, :publication, :container, :created_by, now())');

    for ($i=2; $i<$nr; $i++) {
        // Bind values
        $this->db->bind(':title', $data[$i]['B']);
        $this->db->bind(':author', $data[$i]['C']);
        $this->db->bind(':publication', $data[$i]['D']);
        $this->db->bind(':container', $data[$i]['E']);
        $this->db->bind(':created_by', $_SESSION['user_id']);

        // Execute query
        if ($this->db->execute()) {
            return true;
        } else {
            return false;
        }
    }
}

【问题讨论】:

  • 你是在循环调用方法吗?
  • 是的,我从我的数据库类调用绑定方法
  • 你尝试过什么调试?循环是否从2 正常运行到您期望的数字? bind调用中使用的数据是你期望的吗?
  • 是的,我检查了值,我从 excel 表中循环的行数正确,绑定方法上的值也正确
  • 您在第一次插入“return”后退出函数

标签: php pdo bulkinsert


【解决方案1】:

return statement 将结束您的方法的执行——并退出循环。引用手册:

如果从函数内部调用,则立即返回语句 结束当前函数的执行,并将其参数返回为 函数调用的值。

为了完成这项工作,只需最少的努力,我只会在 $this-&gt;db-&gt;execute() 失败时 return false 在您的方法结束时使用 return true,如下所示:

public function importBooks($data, $nr)
{
    // Init query
    $this->db->query('INSERT INTO books_pre (title, author, publication, container, created_by, created_at) VALUES (:title, :author, :publication, :container, :created_by, now())');

    for ($i=2; $i<$nr; $i++) {
        // Bind values
        $this->db->bind(':title', $data[$i]['B']);
        $this->db->bind(':author', $data[$i]['C']);
        $this->db->bind(':publication', $data[$i]['D']);
        $this->db->bind(':container', $data[$i]['E']);
        $this->db->bind(':created_by', $_SESSION['user_id']);

        // Execute query
        if (!$this->db->execute()) {
            return false;
        }
    }

    return true;
}

但是,如果我必须重写此代码,我个人可能会内爆数据数组并将其全部插入到一个查询中。

【讨论】:

  • 你为什么说你会内爆数据,是不是提高了性能?
  • @Looper 它可能会提高性能,尽管在此示例中不会很明显。但是,如果您有一个更大的数据集(比如说 10,000 行),您将发送大量查询——可能太多、太快。在这种情况下,如果你问我,最好将它插入几个更大的块中。
  • 我有超过 5000 行,那么有没有更好的方法来正确插入这么多?
  • 是的,我会将它插入几个大块中。这个post 对你有帮助吗?
  • 这可能值得提出一个新问题,因为您现在遇到了另一个问题。除非你已经想通了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多