CodeIgniter Active Record 'like' ignoring 'where' - php

riting a very simple search using like and have an option to omit options, however I find the like statement is making query ignore the where statement
$this->db->like('LOWER(location) OR LOWER(name)', strtolower($term));
$this->db->where('stage', 1);
$this->db->order_by("name", "asc");
$query = $this->db->get($this->user_table);
return $query->result();
Example of what the above produces with $term = "dublin";
SELECT * FROM (`users`) WHERE `stage` = 1 AND LOWER(location) OR LOWER(name) LIKE '%dublin%' ORDER BY `name` asc"
It still returns rows where 'stage' is not equal to 1.
Any ideas? Thank you!

$term = strtolower($term);
$this->db->where("(LOWER(location) LIKE '%{$term}%' OR LOWER(name) LIKE '%{$term}%')");

$query = $this->db->query("SELECT id_alimento, nombre, unidad, energia_kcal FROM catalogo_alimentos WHERE LOWER(nombre) LIKE '%".$this->db->escape_like_str($search)."%'");
if($query != false){
if ($query->num_rows() > 0) {
return $query->result();
}
}

Substitute this for the query
$term = strtolower($term);
$this->db->where("stage= 1 AND (LOWER(location) LIKE '%{$term}%' OR LOWER(name) LIKE '%{$term}%')");

Related

Impliment Select Count in Codeigniter and echo the Results

I have the following query I will like to implement in CodeIgniter:
SELECT COUNT(*) FROM tickets WHERE status = "open";
The result returned will be '1' and I will like to echo the result. I have the current code query:
$this->db->count_all_results();
$this->db->select('*');
$this->db->where('status', 'Open');
$this->data['opentickets'] = $this->support_m->get();
I am trying to display the count result within the view. Any advice on how I can do this?
Please try below code to get the count the number of row with open status.
$this->db->where("status", 'Open');
$query = $this->db->get("tickets");
$this->data['opentickets'] = $query->num_rows();
Or You can use this one
$sql = 'SELECT COUNT(*) FROM tickets WHERE status = "open"';
$query = $this->db->query($sql);
$this->data['opentickets'] = $query->row_array()['COUNT(*)'];
You can run query in codeigniter :
$sql = 'SELECT COUNT(*) FROM tickets WHERE status = "open"';
$query = $this->db->query($sql);
$this->data['opentickets'] = $query->result_array();

Codeigniter: Customize ordering query

I want to achieve doing ORDER BY task_status = 'Open'
but I am unable to get the result.
I did this
$this->db->from('session_tasks');
$this->db->order_by('task_status', 'OPEN', 'DESC');
$query = $this->db->get();
I hope anyone can help.
Try this,
$this->db->query("SELECT * FROM session_tasks
ORDER BY task_status = 'OPEN' DESC,task_status ASC");
Alternatively,
$this->db->query("SELECT * FROM session_tasks
ORDER BY CASE WHEN task_status = 'OPEN' THEN 0 ELSE 1 END ASC,
task_status ASC"); //Ordering starts with Open, then in Ascending order
Here is one more solution using Codeigniter active record that works. Note the usage of double quotes when using string literal.
$this->db->_protect_identifiers = FALSE;
$this->db->from('session_tasks');
$this->db->order_by("task_status = 'OPEN'", 'DESC');
$this->db->order_by('task_status');
$query = $this->db->get();
$this->db->_protect_identifiers = TRUE;
use this code
$this->db->order_by("task_status", "desc");
$query = $this->db->get_where('session_tasks', array('task_status'=>'open'));
to check the documentation click here
You can do this like this:
// set this to false so that _protect_identifiers skips escaping:
$this->db->_protect_identifiers = FALSE;
// your order_by line:
$this -> db -> order_by('FIELD ( products.country_id, 2, 0, 1 )');
// important to set this back to TRUE or ALL of your queries from now on will be non-escaped:
$this->db->_protect_identifiers = TRUE;
Source

CodeIgniter Active Record multiple WHERE clauses

I am trying to convert this query:
SELECT * FROM table WHERE condition1 = 'example' AND (date1 = 'date' OR renewal1 = 'renewal');
into CodeIgniter's Active Record format. I tried the following:
$q = $this->db->select('*');
$q = $this->db->from('table');
$q = $this->db->where('condition1 = condition');
$q = $this->db->where('date1 = date OR renewal = renewal');
$q = $this->db->get();
$results = $q->result();
But this did not have the desired affect. It seemed to not place the parenthesis around the second set of conditions, which caused the query to not work as expected. How else can I do this to represent what I want?
Thanks for the help!
You can use
$this->db->select('*');
$this->db->from('table');
$this->db->where('condition1 =',' condition');
$where = '(date1 = "date" OR renewal1 = "renewal")';// you can use your condition like this
$this->db->where($where);
$q = $this->db->get();
$results = $q->result();

PHP query error

I am using LIKE to do my searching, i try it in phpMyAdmin and return the result but when i use it in php it return empty result.
$search = "ip";
$start = 0;
$query = "SELECT * FROM product WHERE product_name LIKE '%$search%' LIMIT $start,30";
$result = mysql_query($query);
if(empty($result))
$nrows = 0;
else
$nrows = mysql_num_rows($result);
It will return result when i using phpMyAdmin to run this query but when i use it in php, it return empty.
Update:
Sorry guys,
I just found out the problem is i didn't connect database as well. anyway, thanks for helping.
Try This
$query = "SELECT * FROM `product` WHERE `product_name` LIKE '%".$search."%' LIMIT 0, 30";
And if the sole purpose of your code is to get the number of products with the searched-for name, use SELECT COUNT(*) instead of doing a mysql_num_rows() on all your data. It will decrease your querytime and the amount of data that is (unnecessarily) fetched.
I am not sure why this is not working, as the query seems to be correct to me. I would like to suggest you writing query this way
$query = <<<SQL
SELECT * FROM product WHERE product_name LIKE "%$search%" LIMIT $start,30
SQL;
please note that there should not be any space or any character after SQL;
$query = "SELECT * FROM product WHERE product_name LIKE '%" . $search . "%' LIMIT " . (int) $start. ",30";
you can use directly mysql_num_rows()
but here is right code
$query = "SELECT * FROM product WHERE product_name LIKE '%".$search."%' LIMIT $start,30";
$search = "ip";
$start = '0';
$query = "SELECT * FROM product WHERE product_name LIKE '%".$search."%' LIMIT $start,30";
$result = mysql_query($query)or die(mysql_error());
if(mysql_num_rows($result) == 0){
$nrows = 0;
} else{
$nrows = mysql_num_rows($result);
}
//use mysql_num_rows($result) instead of empty($result) because in this situation $result is every time not empty so use inbuilt PHP function mysql_num_rows($result);

i want to fetch that from mysql database using codeigniter framework php?

I want to fetch that through model
SELECT `id`, `name`, `visits`, `shortdes`, `photo` FROM `products`
WHERE `name` LIKE '$word'
OR `shortdes` LIKE '$word' ANd `valid` = 1 ORDER BY `id` DESC
I used that, but it returned false
$this->db->order_by('id', 'desc');
$this->db->like('name', $word);
$this->db->or_like('shortdes', $word);
$this->db->where('valid', 1);
$this->db->select('id, name, visits, shortdes, photo');
$query = $this->db->get('products');
What can i use?
For debugging codeigniter's query builder query's, I usually reccommend doing something like this to debug them and see exactly what query the statements are producing.
$this->db->order_by('id', 'desc');
$this->db->like('name', $word);
$this->db->or_like('shortdes', $word);
$this->db->where('valid', 1);
$this->db->select('id, name, visits, shortdes, photo');
$query = $this->db->get('products');
echo "<pre>"
die(print_r($this->db->last_query(), TRUE));
When ran, it will output the actual raw SQL query being produced, and you can then debug it from there.
You should order your active record queries the same way you order a regular SQL query...
SELECT
WHERE
order_by
$this->db->get(x); -- which is the equivalent of FROM.
IE:
<?php
$this->db->select('id, name, visits, shortdes, photo');
$this->db->where('valid', 1);
$this->db->like('name', $word);
$this->db->or_like('shortdes', $word);
$this->db->order_by('id', 'DESC');
$query = $this->db->get('products');
if ($query->num_rows() > 0)
{
$row = $query->row();
echo $row->id;
echo $row->name;
echo $row->visits;
etcetc...
}
Or instead of using active records you can just use a full query.
$query = $this->db->simple_query('YOUR QUERY HERE');
Use the following user guide pages for more documentation:
active records:
http://codeigniter.com/user_guide/database/active_record.html
query results:
http://codeigniter.com/user_guide/database/results.html

Categories