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

Php 将数组的所有行插入MySql表-与Codeigniter相关,php,mysql,sql,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

我正在尝试输入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上运行这个。Error msg位于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);

这应该可以完成您试图完成的任务。

错误消息几乎是不言自明的

  • 在代码的第2行中,您将$recipient_数组声明为数组(),而不是对象。因此,没有可用的“结果”方法。 你的循环应该是

    foreach($recipient\u数组作为$row)

  • 另一方面,您可能不应该在循环内执行db操作(特别是对于100个操作!)。相反,您应该将所有查询保存在一个大查询字符串中,并在最后执行


如果您希望您的100个电子邮件地址以“,”分隔,则应使用内爆(胶水、碎片)而不是explode()函数。如您要插入的电子邮件地址

$recipient_raw = array();
$recipient_raw[] = $this->input->post('recipient');

foreach ($recipient_raw as $value) {

        $tem_arr = implode(',', $value);
    }

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