How to insert records using select in codeigniter active record

扶醉桌前 提交于 2019-12-03 08:25:22

I think you are talking about a a SELECT ... INSERT query, on the active record class there is not a method to do that, but there are two ways to do it

1)

$query = $this->db->query('INSERT california_authors (au_id, au_lname, au_fname)
                           SELECT au_id, au_lname, au_fname
                           FROM authors
                           WHERE State = \'CA\'');

As you say

And 2) you can can do this, using what Calle said,

$select = $this->db->select('au_id, au_lname, au_fname')->where('state', 'CA')>get('california_authors');
if($select->num_rows())
{
    $insert = $this->db->insert('california_authors', $select->result_array());
}
else
{ /* there is nothing to insert */
$query = $this->db->insert('california_authors', array('au_id' => 'value', 'au_lname' => 'value', 'au_name' => 'value'));

$query2 = $this->db->select('au_id, au_lname, au_fname')->where('state', 'CA')->get('california_authors');

To retrieve the result you can do this:

$resultarr = $query->result_array(); // Return an associative array

There is a lot of information on this in the manual.

http://codeigniter.com/user_guide/database/active_record.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!