// Load the database library in CodeIgniter framework $this->load->database(); // Get the instance of the database object $db = $this->db->get_instance(); // Use the database object to perform a query $query = $db->query("SELECT * FROM users"); // Display the results of the query foreach ($query->result() as $row) { echo $row->name; }
// Load the database library in CodeIgniter framework $this->load->database(); // Get the instance of the database object $db = $this->db->get_instance(); // Start a transaction $db->trans_start(); // Insert a new user into the database $data = array( 'name' => 'John Doe', 'email' => 'john.doe@example.com' ); $db->insert('users', $data); // End the transaction $db->trans_complete(); // Check if the transaction was successful if ($db->trans_status() === FALSE) { // Rollback the transaction $db->trans_rollback(); echo 'Transaction failed.'; } else { // Commit the transaction $db->trans_commit(); echo 'Transaction succeeded.'; }This example shows how to get an instance of the database object using the get_instance() method in CodeIgniter and then use it to perform a transaction to insert a new user into the database. The code checks if the transaction was successful and either commits or rolls back the transaction accordingly. Package library: CodeIgniter Database Library.