Initialize the database class the following code will load and initialize the database class according to your:
$this->load->database ();
Multi-result standard query (object form)
$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 ();
the result () function above returns an array of objects. Example: Multi-result standard query, $row (array form)
$query = $this->db->query (' SELECT name, title, email from my_table '); foreach ($query->result_array () as $row) { C0/>echo $row [' title ']; echo $row [' name ']; echo $row [' email '];}
the Result_array () function above returns an array with the subscript. For example: $row ['title' test Results If your query may not return results, we recommend that you first use the Num_rows () function to test:
$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; }}
Single-result standard query (object form)
$query = $this->db->query (' SELECT name from my_table LIMIT 1 '); $row = $query->row (); Echo $row->name;
the row () function above returns an object. Example: $row single result standard query (array form)
$query = $this->db->query (' SELECT name from my_table LIMIT 1 '); $row = $query->row_array (); Echo $row [' name '];
the Row_array () function above returns an array. Example: $row ['name' standard insertion (insert)
$sql = "INSERT into MyTable (title, name) VALUES (". $this->db->escape ($title). ",". $this->db->escape ($ Name). ")"; $this->db->query ($sql); Echo $this->db->affected_rows ();
Fast Query Fast query class can provide us with a quick way to obtain data:
$query = $this->db->get (' table_name '); foreach ($query->result () as $row) { echo $row->title;}
Shortcut Insert (INSERT)
$data = Array ( ' title ' = = $title, ' name ' = = $name, ' date ' = $date ); $this->db->insert ( ' MyTable ', $data);//equivalent: INSERT into MyTable (title, name, date) VALUES (' {$title} ', ' {$name} ', ' {$date} ')
Example of incremental operation of database links