I am using LIKE to see if the similar order_id exists or not in my db. When i run the application, i see that this below code doesnt return any value. It is returning only null value.
$temp_orders = $this->transaction_model->get_similar_user_temp_transaction($cart_order_id);
function get_similar_user_temp_transaction($order_id)
{
$query = $this->db->select('*')
->from('temp_transaction')
->like('order_id', $order_id)
->get();
return ($query->num_rows() < 1) ? null : $query->result();
}
Kindly check the above code. Help would be appreciated
try your code after changing statement order
function get_similar_user_temp_transaction($order_id)
{
$query = $this->db->select('*')
->like('order_id', $order_id)
->from('temp_transaction')
->get();
return ($query->num_rows() < 1) ? null : $query->result();
}
EDIT
If you want to match exact order_id, use where instead of like
function get_similar_user_temp_transaction($order_id)
{
$query = $this->db->select('*')
->where('order_id', $order_id)
->from('temp_transaction')
->get();
return ($query->num_rows() < 1) ? null : $query->result();
}
write like in where cluase
$query = $this->db->select('*')
->from('temp_transaction')
->where("order_id LIKE '%$order_id%'")->get();
Here SQL like not work, because like is not working with INT values
You have two options :-
1. ->like(CONVERT(order_id, CHAR(16), $order_id)
2. Use ->where(order_id, $order_id)
If you are trying to match exact use where clause.
//dont need ->select('*') //excluding select gives same equivalent
//dont need from
return $this->db->where('order_id', $order_id)->get('temp_transaction')->result();
Related
My Code :
public function amil_export($program){
$query = $this->get_where('tbl_amil', [ 'jenis_lembaga' => '%Perorang%',
'program' => $program
]);
$query = $this->execute();
return $query;
}
And I want to make Query in SQL like This
SELECT * FROM `tbl_amil` WHERE `jenis_lembaga` LIKE '%Perorang%' OR `program` = $program
can Someone fix my code
I've got a solution for this. I've used $this->db->group_start(); and $this->db->group_end();
public function amil_export($keyword, $program) {
$this->db->select('*');
$this->db->group_start();
$this->db->like('jenis_lembaga',$keyword);
$this->db->or_like('program',$program);
$this->db->group_end();
$query = $this->db->get('tbl_amil');
// echo $this->db->last_query();
return $query->result_array();
}
Hope this helps!!
get_where will not be able to give you the result with OR condition in it. (REFERENCE)
You can use like and or_where to get the query as you want, like so -
$query = $this->db->like('jenis_lembaga', 'Perorang')->or_where('program', $program)->get('tbl_amil')->result();
// Produces:
// SELECT * FROM `tbl_amil` WHERE `jenis_lembaga` LIKE '%Perorang%' ESCAPE '!' OR `program` = $program
You can also use query grouping (REFERENCE) as #Ankit Jindal suggests.
Hope it helps you.
public function amil_export($program)
{
$this->db->like('jenis_lembaga ','Perorang','both');
$this->db->or_where('program',$program);
$query=$this->db->get('tbl_amil');
return $query->result();
}
Im trying to get the next row everytim ei run the function but it always returns 0
Model
function getNextMuziek($id)
{
$this->db->where('id', $id);
$this->db->limit(1);
$query = $this->db->get('muziek')->num_rows() == 1;
return $query->next_row();
}
According to the documentation, next_row() is to be used to navigate through a result set. This will not serve your purposes. If you want your function to return the next row after the row specified by $id, the easiest thing would be to use a greater than comparison and return the first row. You could modify your code to
$query = $this->db->where('id >', $id)->limit(1)->get('muziek');
if ($query->num_rows() == 1) {
return $query->row();
}
I am working in codeigniter.. I want to count the no. of rows having the same order_no.. below is my code.
public function get_my_orders() {
$user_data = $this->session->all_userdata();
$email = $user_data['user_email'];
$this->db->select('*');
$this->db->from('order_details');
$this->db->where('email', $email);
$this->db->group_by('order_no');
$this->db->order_by('order_no', 'DESC');
$query = $this->db->get();
return $query->result();
}
Pls help..
You can use $this->db->count_all_results()
public function get_my_orders() {
$user_data = $this->session->all_userdata();
$email = $user_data['user_email'];
//$this->db->select('*');// no need select as you only want counts
$this->db->from('order_details');
$this->db->where('email', $email);
//$this->db->group_by('order_no');//no need group_by as you only want counts
//$this->db->order_by('order_no', 'DESC');//no need order_by as you only want counts
return $this->db->count_all_results();;
}
Try changing your select line to look like this:
$this->db->select('COUNT(*) as count');
Rather than having all fields accessible like they currently are, you will instead only have access to one variable called count. To keep all variables accessible and add the count in as well, use this instead:
$this->db->select('*, COUNT(*) as count');
Feel free to change the lowercase name for count, I'm just using that as an example.
Check Codeigniter Manual
$query = $this->db->get();
return $query->num_rows(); //Return total no. of rows here
$query->num_rows(); will count the total active record return by your query.
I wrote this code for search in my table search for a string in posts where show = 1.
but when i run this code where('show',1) not affected and rows where having show = 0 returned from database, what this problem?
public function json_search($str)
{
$out['Threadt'] = $this->db
->select('id')
->from('posts')
->like('title',$str)
->or_like('story',$str)
->or_like('description',$str)
->or_like('full-story',$str)
->where('date <',time())
->where('show',1)
->get()
->result_array();
}
you should try this, this should work:
function json_search($str)
{
$where = "( story like '%".$str."%' OR title like '%".$str."%' OR description like '%".$str."%' OR full-story like '%".$str."%' )";
$out['Threadt'] = $this->db
->select('id')
->from('posts')
/*->like('title',$str)
->or_like('story',$str)
->or_like('description',$str)
->or_like('full-story',$str)*/
->where( $where, false, false )
->where('date <',time())
->where('show',1)
->get()
->result_array();
}
See the commented part above, when you're using it like this, you are appending OR clauses and this interferes with the show clause.
I am trying to do this sql query on codeigniter
SELECT* FROM person WHERE type = 'staff' AND description LIKE 'university%'
I am not sure if it is correct...
$this->db->get_where('person' , array('type'=>'staff'))
->like('description','university%')
->result_array();
does anyone have an idea about my case? thanks in advance ...
Using Active Record you can do so
$query = $this->db->select('*')
->from('person')
->where('type','staff')
->like('description','university','after')
->get();
$result = $query->result_array();
Make sure you pass after as a third parameter in like() function so active record will add the wild card i.e % after university so it will look like LIKE 'university%'
The docs has this explained very well, but their API is very simple. To produce the query you need the relevant code which looks like this:
$this->db->select('*')
->from('person');
->where('type', 'staff')
->like('description','university');
$query = $this->db->get();
$result = $query->result_array();
I never used chaining(even though i know it's possible), but breaking down your question should be easy ;
$this->db->from('person');
$this->db->where('type','staff');
$this->db->where('description', 'university%');
$query = $this->db->get();
$result = $query->result_array();
return $result;