I'm trying to use a like
query on Codeigniter. The function escapes my percent sign %
to \%
.
My query is:
SELECT *
FROM (`invoices_rent`)
WHERE `clerk_name` = 'BEAUTY'
AND `date` LIKE '\%/\%/\%'
$this->db->like('date', $date, 'none');
What can I do to prevent the percent sign getting escaped?
I'm trying to make a filter with the date where i can receive a number or the wildcard. So what i want is to join this data in a way that i can get the month, year or date that the user wants. So i would pass to the query "%/02/2015" or "%/%/2015" to return the data range that i need. But I'm having a problem with the escape on the function. I think I'll need to create the entire query instead of using this CodeIgniter functions to make it. Follow the actual model function.
$date = "%/03/2015";
public function getallinvoices($type = false, $date = false, $clerk = false)
{
if($type == "m")
$table = "invoice_month";
else
$table = "invoices_rent";
if($date != false)
$this->db->like('date', $date, 'none');
if($clerk != false && $clerk != "all")
$this->db->where('clerk_name', $clerk);
$query = $this->db->get($table);
$this->output->enable_profiler(TRUE);
print_r($query);
return $query->result_array();
}
I want the query to return like:
SELECT *
FROM (`invoices_rent`)
WHERE `clerk_name` = 'BEAUTY'
AND `date` LIKE '%/03/2015'
I'm not sure what exactly you are trying to do here.
Adding a 3rd parameter as none
won't add wildcard (%)
around the value (or it may escape
them, in your case).
From docs:
If you do not want to use the wildcard (%) you can pass to the optional third argument the option 'none'.
$this->db->like('title', 'match', 'none');
// Produces: WHERE title LIKE 'match'
Hence if you want to use wildcard, just remove the 3rd parameter.
(just for your reference)
Assuming table,
`invoices_rent`
id rent invoice_number clerk_name date
1 150 INV001 BEAUTY 2015-03-04
2 250 INV002 BEAUTY01 2015-02-05
3 350 INV003 BEAUTY 2015-03-04
Query,
$date = '2015-03-04';
$this->db->like('date', $date);
$this->db->where('clerk_name');
$query = $this->db->get('invoices_rent');
return $query->result();
/**
* Result will give id: 1 & 3 as output
*/
EDIT:
As per your updated question, basically you want all the rows of the month march, 2015. What you are trying is definitely not the right way to query when date ranges are in concerned. What you can do is like get the start of the month (for eg. 2014-03-01) and end of the month (2014-03-31) and use between clause
and this will give you all the rows of the month March
Below will be your query.
$month_start = date('Y-m-01', 'March'); # Month's start
$month_end = date('Y-m-t', 'March'); # Month's end
$this->db->where('date >=', $month_start);
$this->db->where('date <=', $month_end);
$this->db->where('clerk_name');
$query = $this->db->get('invoices_rent');
return $query->result();
/**
* Result will give id: 1 & 3 as output
*/