Wpdb->一次插入多条记录

时间:2014-03-03 作者:user759235

因此,在创建了表“settings table”之后,我想向其中添加几条记录。

这是我想做的,不是最好的方法,所以我需要一个更好的方法来做,因为我将向表中添加更多记录。

示例1

 $wpdb->insert(\'settings-table\', array(\'option_name\'   => \'name-1\', 
                                       \'option_value\'  => \'val-1\', 
                                       \'option_created\'=> current_time(\'mysql\'),
                                       \'option_edit\'   => current_time(\'mysql\'),
                                       \'option_user\'   => \'user-1\' 
                                       ));    
 $wpdb->insert(\'settings-table\', array(\'option_name\'   => \'name-2\', 
                                       \'option_value\'  => \'val-2\', 
                                       \'option_created\'=> current_time(\'mysql\'),
                                       \'option_edit\'   => current_time(\'mysql\'),
                                       \'option_user\'   => \'user-2\' 
                                       ));
 $wpdb->insert(\'settings-table\', array(\'option_name\'   => \'name-1\', 
                                       \'option_value\'  => \'val-3\', 
                                       \'option_created\'=> current_time(\'mysql\'),
                                       \'option_edit\'   => current_time(\'mysql\'),
                                       \'option_user\'   => \'user-3\' 
更新

这很有效(欢迎使用其他更好的解决方案)

$wpdb->query("INSERT INTO settings-table
            (`option_name`, `option_value`, `option_created`, `option_edit`, `option_user`)
            VALUES
            (\'name-1\', \'val-1\', current_time(\'mysql\'), current_time(\'mysql\'), \'user-1\'),
            (\'name-2\', \'val-2\', current_time(\'mysql\'), current_time(\'mysql\'), \'user-2\'),
            (\'name-3\', \'val-3\',  current_time(\'mysql\'), current_time(\'mysql\'), \'user-3\')")

4 个回复
SO网友:rajesh

$wpdb->query("INSERT INTO settings-table
            (option_name, option_value, option_created, option_edit, option_user)
            VALUES
            (\'name-1\', \'val-1\', current_time(\'mysql\'), current_time(\'mysql\'), \'user-1\'),
            (\'name-2\', \'val-2\', current_time(\'mysql\'), current_time(\'mysql\'), \'user-2\'),
            (\'name-3\', \'val-3\',  current_time(\'mysql\'), current_time(\'mysql\'), \'user-3\')")
在您发布的查询中,列名不应为字符串。

SO网友:user54858

$wpdb->insert 函数不支持在单个函数调用中插入多条记录。您必须循环您的数据并准备要插入的数据,然后使用$wpdb->插入循环并逐个插入记录。

相反,我们可以按如下方式准备插入查询,并只运行一次查询。

如果我们想插入100 记录,我们需要运行100 insert queries 如果我们想使用$wpdb->insert. 如果我们使用以下代码,我们只需要运行1 insert query.

希望这有帮助。

function do_insert($place_holders, $values) {

    global $wpdb;

    $query           = "INSERT INTO settings-table (`option_name`, `option_value`, `option_created`, `option_edit`, `option_user`) VALUES ";
    $query           .= implode( \', \', $place_holders );
    $sql             = $wpdb->prepare( "$query ", $values );

    if ( $wpdb->query( $sql ) ) {
        return true;
    } else {
        return false;
    }

}

$data_to_be_inserted = array( array(
    \'option_name\'   => \'name-1\', 
    \'option_value\'  => \'val-1\', 
    \'option_created\'=> current_time(\'mysql\'),
    \'option_edit\'   => current_time(\'mysql\'),
    \'option_user\'   => \'user-1\' 
),

array(
    \'option_name\'   => \'name-2\', 
    \'option_value\'  => \'val-2\', 
    \'option_created\'=> current_time(\'mysql\'),
    \'option_edit\'   => current_time(\'mysql\'),
    \'option_user\'   => \'user-2\' 
),
array(
    \'option_name\'   => \'name-1\', 
    \'option_value\'  => \'val-3\', 
    \'option_created\'=> current_time(\'mysql\'),
    \'option_edit\'   => current_time(\'mysql\'),
    \'option_user\'   => \'user-3\'
));

$values = $place_holders = array();

if(count($data_to_be_inserted) > 0) {
    foreach($data_to_be_inserted as $data) {
        array_push( $values, $data[\'option_name\'], $data[\'option_value\'], $data[\'option_created\'], $data[\'option_edit\'], $data[\'option_user\']);
        $place_holders[] = "( %s, %s, %s, %s, %s)";
    }

    do_insert( $place_holders, $values );
}

SO网友:johnnitro

这也会起作用

        $nameArray = array("name-1", "name-2", "name-3");
        $valueArray = array("val-1", "val-2" , "val-3");
        $userArray = array("user-1" , "user-2", "user-3");
        $maxRow= count($nameArray); // can use count of any array
        $setting_values = array();
        $time = current_time(\'mysql\');

        for($x=0;$x<$maxRow; $x++){
            $SettingContext = [
                \'option_name\' => "\'" . $nameArray[$x] . "\'",
                \'option_value\' => "\'" . $valueArray[$x] . "\'",
                \'option_created\' => "\'" . $time . "\'",
                \'option_edit\' => "\'" . $time . "\'",
                \'option_user\' => "\'" . $userArray[$x] . "\'"
            ];
            $setting_values[] = "(" . implode(\',\', $SettingContext) . ")";
        }
        $settingSubmit = implode(\',\', $setting_values);
        $setting_save = $wpdb->query("
                INSERT INTO setting
                (option_name, option_value, option_created,option_edit, option_user)
                VALUES " . $settingSubmit
        );

SO网友:Kodie Grantham

我提出了这个解决方案,它扩展了wpdb 类,以便使用其内部数据处理函数:

<?php
class wpdbx extends wpdb {
  public function __construct() {
    parent::__construct(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
  }

  public function insert_multiple($table, $data, $format = null) {
    $this->insert_id = 0;

    $formats = array();
    $values = array();

    foreach ($data as $index => $row) {
      $row = $this->process_fields($table, $row, $format);
      $row_formats = array();

      if ($row === false || array_keys($data[$index]) !== array_keys($data[0])) {
        continue;
      }

      foreach($row as $col => $value) {
        if (is_null($value[\'value\'])) {
          $row_formats[] = \'NULL\';
        } else {
          $row_formats[] = $value[\'format\'];
        }

        $values[] = $value[\'value\'];
      }

      $formats[] = \'(\' . implode(\', \', $row_formats) . \')\';
    }

    $fields  = \'`\' . implode(\'`, `\', array_keys($data[0])) . \'`\';
    $formats = implode(\', \', $formats);
    $sql = "INSERT INTO `$table` ($fields) VALUES $formats";

    $this->check_current_query = false;
    return $this->query($this->prepare($sql, $values));
  }
}

global $wpdbx;
$wpdbx = new wpdbx();
?>
然后,您可以这样使用它:

<?php
global $wpdbx;
$results = $wpdbx->insert_multiple(
  \'settings-table\',
  array(
    array(
      \'option_name\'   => \'name-1\', 
      \'option_value\'  => \'val-1\', 
      \'option_created\'=> current_time(\'mysql\'),
      \'option_edit\'   => current_time(\'mysql\'),
      \'option_user\'   => \'user-1\' 
    ),
    array(
      \'option_name\'   => \'name-2\', 
      \'option_value\'  => \'val-2\', 
      \'option_created\'=> current_time(\'mysql\'),
      \'option_edit\'   => current_time(\'mysql\'),
      \'option_user\'   => \'user-2\' 
    ),
    array(
      \'option_name\'   => \'name-1\', 
      \'option_value\'  => \'val-3\', 
      \'option_created\'=> current_time(\'mysql\'),
      \'option_edit\'   => current_time(\'mysql\'),
      \'option_user\'   => \'user-3\'
    )
  )
);
?>

结束

相关推荐

自更新至3.8以来,“MySQL服务器已消失”

我在专用服务器上安装了WordPress,该服务器内存充足(超过8GB)。WordPress安装包括许多插件-50个活动插件。最需要注意的是WPML 使WordPress成为多语言的插件。主题是Weaver II Pro.堆栈是LAMP-CentOS 6.3、Apache 2.4.4、Percona MySQL、PHP-FPM。我一直很笨,没有设置测试或临时服务器。在我更新到WordPress 3.8之前,一切都很好,同时还更新了几个插件。当时我没有注意,但我所有的小部件都搞砸了,任何导航都搞砸了。我启用