Category Archives: Codeigniter
Codeigniter Delete Query Example
Codeigniter delete query example : Codeigniter inbult query bulder $this->db->delete() is used to delete the data from table. Codeigniter also provides $this->db->empty_table() and $this->db->truncate() method to delete all data from table. $this->db->delete() deletes data based on where condition, it will delete the rows where condition is fullfilled. Let us understand how we can use these query builders to delete data from tables.
Delete data in Codeigniter | Truncate Table | Remove All Data
$this->db->delete() is used to delete data in codeigniter table.
Codeigniter delete query example
Let us go one by one to understand the delete functionality in Codeigniter.
Delete With Where Condition
You can delete the data based on condition using the below example-
$where_array = array( 'id'=>$id ); $this->db->delete('table_name', $where_array);
or You can also use this :
$where_array = array( 'id'=>$id ); $this->db->where($where_array); $this->db->delete('table_name');
This will generate the following Query :
// Delete from table_name WHERE id = $id
Using where condition you can delete Multiple Rows from table.
Empty Table | Delete All Rows
You can use the following syntax to empty any table. It will delete all rows from table.
$this->db->empty_table('users_table');
It will produce query like this – DELETE from users_table
Note: – It will delete all records from table so before running the above query make sure you really want to delete all rows.
Truncate Table
$this->db->truncate('users_table');
It will produce query like this – TRUNCATE users_table;
Note: – It will truncate table & remove all indexes as well so make sure you really want to truncate table.
Codeigniter insert Query example
Codeigniter Insert Query Example :
Insert data in Codeigniter
$this->db->insert() is used to insert data in codeigniter table.
Codeigniter Insert Query Example
$data = array( 'id' => $id, 'name' => $name, 'email' => $email, 'phone' => $phone ); $this->db->insert('table_name', $data);
This will generate the following Query :
// INSERT INTO table_name (id,name,email,phone)values($id,$name,$email,$phone);
Codeigniter update query example
Codeigniter update query: We can use Codeigniter Query builder class to update data in table. Codeigniter provides classes to interact with database. Using query builder classes you can perform update action by simply writing few lines of code. While writing any query to insert, update or delete we always need to take care about sql injection and other security things. Here in this article we are going to explain how you perform update operation on database using query builder class.
Codeigniter update query With Where Condition | Example
We will create an example to update with where condition and multiple columns.
Here is simple example to update name, email and phone number in table – my_table using where condition.
$this->db->update() is used to update data in codeigniter table.
$data = array( 'name' => $name, 'email' => $email, 'phone' => $phone ); $this->db->where('id', $id); $this->db->update('my_table', $data);
This will generate the following Query :
Codeigniter update query Where example : Output
// UPDATE my_table // SET name = '{$name}', email= '{$email}',phone = '{$phone}' // WHERE id = $id
Note : You can also pass objects instead of passing array ie. $data.
You can get more details about the query builder class in codeigniter.
Read >> Query Builder CLass
Read >> Query Builder CLass Documentaion
Return Value –
>Update query return value– You can use the below syntax to check whether update is successful or not-
if($this->db->affected_rows() > 0){ // update successful }else{ // update failed }
Multiple Where Condition
You can add multiple where condition in query simply as below-
$email = 'youremail@yopmail.com'; $id = 10; $data = array( 'name' => $name, 'phone' => $phone ); $this->db->where('id', $id); $this->db->where('email', $email); // on the same way you can add other conditions $this->db->update('my_table', $data);
Codeigniter get_where example
Codeigniter get_where example : This is basically used to get data based on where condition. Let us create a simple example to understand how “where” clause works in Codeigniter.
Codeigniter get_where Condition
Codigniter $this->db->get_where() allows you to create a sql slect query having the WHERE Clause.
$query = $this->db->get_where($table_name,$where_array, $limit, $offset);
$table_name – Your table name.
$where_array – Array which generates the condition.
$limit – no of records to be loaded.
$offset – start of limit.
Codeigniter get_where With Multiple Conditions example
You can use get_where in codeiniter to add one or multiple conditions. Here in this examplewe have added two conditions in where clause.
$where_array = array( 'email'=>'test@gmail.com', 'status'=>'1' ); $table_name = "users"; $limit = 10; $offset = 0; $query = $this->db->get_where($table_name,$where_array, $limit, $offset);
If you run the above example it will generate the following query:
SELECT * from users where email='test@gmail.com' AND status ='1' limit 0, 10;
On the same way you can add multiple conditions.
Codeigniter pagination example
Codeigniter pagination example :
Codeigniter pagination is very easy to use and customize it.
A working example for codeigniter pagination can be as :
Codeigniter pagination example code
$this->load->library('pagination'); //this is the path of your current page in which you want to apply pagination $config['base_url'] = 'http://demo.com/index.php/test/page/'; $config['total_rows'] = 600;//this total count of rows returned from the query $config['per_page'] = 10; //data per page you want to display. $this->pagination->initialize($config); echo $this->pagination->create_links(); |
Note : You can echo $this->pagination->create_links(); in your view which you are loading in your controller.
Note : By default, It is assumed that you are using URI Segments, and constructs your links something like
http://demo.com/index.php/test/page/20
If you have set
$config['page_query_string'] = TRUE;
Then Your URI Should be like :
http://demo.com/index.php?c=test&m=page&per_page=20
Note : You can use per_page value as your start limit in controller function’s query.
Codeigniter Get Last Insert Id
Codeigniter get last insert id by Query : It is quite simple and easy to get the last inserted id in Codeigniter. $this->db->insert_id(); is used to get the last insert id in codeigniter. It returns the ID Number(primary key) of the last insert record in the table. Let us see how to get last inserted row from table.
Codeigniter get last insert id | Query | Last Inserted row Id Example
$this->db->insert_id(); gives the last inserted record’s id. Run this after insert query to get the last inserted id. Here is simple example of insert query and get last insert id.
Codeigniter get last inserted id
$data = array('name' => $name, 'email' => $email); $this->db->insert_string('users', $data); $id = $this->db->insert_id(); // Will return the last insert id. |
So you can use the above syntax to get last insert id after inserting any new record in database table.
Set default timezone in codeigniter
Set default timezone in codeigniter : date_default_timezone_set() is used to set the default timezone in codeigniter.
Best way to set set default timezone in codeigniter is add date_default_timezone_set(‘America/Los_Angeles’) in index.php
Set default timezone in codeigniter
/*add this in the top of index.php located at the root of application.*/ date_default_timezone_set('America/Los_Angeles'); |
Another way to set default timezone in codeigniter is Add the
Set default timezone in codeigniter controller
|
Codeigniter get current controller
Codeigniter get current controller
You can get Current controller in codeigniter as below :
Codeigniter Current Url
$this->router->fetch_class(); // will return currunt controller name $this->router->fetch_method();// will return currunt method name |
Codeigniter session example
Codeigniter session example
For using codeigniter session you need to load session library . If it already loaded in autoload.php ingore loading again.
Codeigniter Load Session Library
$this->load->library('session'); |
There are two ways to create session in codeigniter.
First : Using associative array
Codeigniter Set Session
$newsessdata = array( 'username' => 'john', 'email' => 'john@gmail.com' ); $this->session->set_userdata($newsessdata); |
Second : Individual variable
Codeigniter Set Session
$this->session->set_userdata('some_variable_name', 'some_value'); |