php - 将数组的所有行插入MySql表 - Codeigniter相关

标签 php mysql sql codeigniter

我正在尝试输入 100 个电子邮件地址,以“;”分隔并将它们存储在 MySql 表中。到目前为止我尝试过的是:

    $recipient_raw = $this->input->post('recipient'); //get the 100 emails in $recipient_raw
    $recipient_array=explode(';', $recipient_raw);  //explode them into an array
    $title = $this->input->post('title');
    $body = $this->input->post('body');

    foreach($recipient_array->result() as $row): 
    $recipient=array(
    'email'=>$row->email             //looping through each email; seems I should not use $row->email since there's no title for them
    );

    $this->db->insert('eamil_send',$this->db->escape($recipient));
    endforeach;

我在 CodeIgniter PHP 上运行它。错误消息位于 foreach($recipient_array->result() as $row): 行,其中显示:在非对象上调用成员函数 result()

如有任何建议,我们将不胜感激!谢谢!

最佳答案

result(),是一个用于转换数据库结果对象的事件记录函数。您正在创建一个标准数组。

$batch = array();
foreach($recipient as $row){
    $batch[] = array(
        'email' => $row
    );
}
$this->db->insert_batch('email_send', $batch);

这应该可以完成您想要完成的任务。

关于php - 将数组的所有行插入MySql表 - Codeigniter相关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24582190/

相关文章:

php - #1242 - 子查询返回超过 1 行

php - 更新一列中的完整数组值

php - iScroll.js 和 iOS 浏览器检测 - 仅向 iPad/iPhone 显示 iScroll.js 文件

java - SQL错误,当我在JAVA代码中实现加入

SQL 匹配字母和数字排列

php - 使用 foreach codeigniter 从数据库中获取数据

c# - Asp.net(查找 2 个日期之间的记录)仅向远程服务器显示错误并在本地主机中正常工作

mysql - MySQL 和 MariaDB 之间的不兼容性 - FROM 子句中的子查询。

Mysql查询动态将行转换为列

sql - 如何将单元格返回到sql函数中的变量中