CodeIgniter Forums
Generating SQL statements - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Generating SQL statements (/showthread.php?tid=35704)



Generating SQL statements - El Forum - 11-08-2010

[eluser]pyronate[/eluser]
Pardon if there is a simple way to do this but I am new to CodeIgniter Smile. What’s the best way to delete a bunch of id’s from a table using the $this->db->delete function? I currently get a bunch of "components" from a view and from the model I get the following array contained in components[] and is as such:

Code:
Array
(
    [0] => 57
    [1] => 58
    [2] => 56
    [3] => 38
    [4] => 46
    [5] => 34
    [6] => 52
    [7] => 53
    [8] => 44
    [9] => 49
    [10] => 41
    [11] => 43
    [12] => 45
    [13] => 47
    [14] => 48
    [15] => 50
    [16] => 51
    [17] => 55
    [18] => 39
    [19] => 37
)
What’s the best way to generate the SQL query "DELETE from components where id=57 etc…" using code igniter?


Generating SQL statements - El Forum - 11-08-2010

[eluser]cahva[/eluser]
Code:
$this->db->where_in('id',$array)->delete('components');

That would create SQL to something like this:
Quote:DELETE FROM components WHERE id IN (57,58,56,...);