Created
July 2, 2012 17:24
-
-
Save jrvaja/3034439 to your computer and use it in GitHub Desktop.
CodeIgniter: DB_Query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /************************************************************************* | |
| Query With Multiple Results | |
| **************************************************************************/ | |
| /*--------------------------------------------------------------*/ | |
| Obejct Version | |
| /*--------------------------------------------------------------*/ | |
| $query = $this->db->query('SELECT name, title, email FROM my_table'); | |
| foreach ($query->result() as $row) | |
| { | |
| echo $row->title; | |
| echo $row->name; | |
| echo $row->email; | |
| } | |
| echo 'Total Results: ' . $query->num_rows(); | |
| /*--------------------------------------------------------------*/ | |
| Array Version | |
| /*--------------------------------------------------------------*/ | |
| $query = $this->db->query("YOUR QUERY"); | |
| if ($query->num_rows() > 0) | |
| { | |
| foreach ($query->result() as $row) | |
| { | |
| echo $row->title; | |
| echo $row->name; | |
| echo $row->body; | |
| } | |
| } | |
| /************************************************************************* | |
| Query With Single Results | |
| **************************************************************************/ | |
| $query = $this->db->query('SELECT name FROM my_table LIMIT 1'); | |
| $row = $query->row(); | |
| echo $row->name; | |
| /*--------------------------------------------------------------*/ | |
| Array Version | |
| /*--------------------------------------------------------------*/ | |
| $query = $this->db->query('SELECT name FROM my_table LIMIT 1'); | |
| $row = $query->row_array(); | |
| echo $row['name']; | |
| /************************************************************************* | |
| Active Records | |
| **************************************************************************/ | |
| /*--------------------------------------------------------------*/ | |
| Insert | |
| /*--------------------------------------------------------------*/ | |
| ******************************************************************* | |
| ********STANDARD FORM******** | |
| ******************************************************************* | |
| $sql = "INSERT INTO mytable (title, name) | |
| VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")"; | |
| $this->db->query($sql); | |
| echo $this->db->affected_rows(); | |
| ******************************************************************* | |
| ********ACTIVE******** | |
| ******************************************************************* | |
| $data = array( | |
| 'title' => $title, | |
| 'name' => $name, | |
| 'date' => $date | |
| ); | |
| $this->db->insert('mytable', $data); | |
| ******************************************************************* | |
| ********insert_string****** | |
| ******************************************************************* | |
| $data = array('name' => $name, 'email' => $email, 'url' => $url); | |
| $str = $this->db->insert_string('table_name', $data); | |
| /*--------------------------------------------------------------*/ | |
| Update | |
| /*--------------------------------------------------------------*/ | |
| $data = array('name' => $name, 'email' => $email, 'url' => $url); | |
| $where = "author_id = 1 AND status = 'active'"; | |
| $str = $this->db->update_string('table_name', $data, $where); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment