How to insert records using select in codeigniter active record
Asked Answered
F

4

5

I want to implement a sql query using CodeIgniter Active Record class. The query looks like this..

INSERT california_authors (au_id, au_lname, au_fname)
SELECT au_id, au_lname, au_fname
FROM authors
WHERE State = 'CA'

Is this possible in CodeIgniter without using the $this->db->query method?

Solution:

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

if($query->num_rows()) {
    $new_author = $query->result_array();

    foreach ($new_author as $row => $author) {
        $this->db->insert("authors", $author);
    }           
}

Regards

Frig answered 29/7, 2010 at 8:29 Comment(0)
L
12

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 */
Limpid answered 29/7, 2010 at 12:56 Comment(1)
I think there needs to be a foreach as well inside the if($select->num_rows())Frig
H
1

if you want to have good control on query execution then you could do your SELECT ... INSERT in 3 ways:

1)use codeigniter active record insert_batch(ci3) or insertBatch(ci4)(recomended):

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

2)use codeigniter active record simple insert:

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

3)use codeigniter active record query execution:

$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\'');
Hillie answered 2/12, 2020 at 11:16 Comment(0)
D
0

it's an old post but this may be usefull to someone.

It's the same as Edgar Nadal answer, with safer way to pass parameter to query

$state = 'CA';
$sql = "
INSERT california_authors (au_id, au_lname, au_fname)
SELECT au_id, au_lname, au_fname
FROM authors
WHERE State = ?
";
$this->db->query($sql, array($state));

codeigniter-3.2.1

Dative answered 7/11, 2020 at 12:53 Comment(0)
T
-2
$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

Tantalic answered 29/7, 2010 at 8:48 Comment(3)
Why did you accept this? It is clearly wrong. Calle is showing you how to do two different statements entirely, not a INSERT...SELECT query.Gretchen
I agree, this shouldn't have been accepted because I misunderstood the question. Edgar Nadal's way is the way to go. srry.Tantalic
@Phil - I tried the code and realized that the solution Calle gave wasn't working.Frig

© 2022 - 2024 — McMap. All rights reserved.