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
Related
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();
Using PHP 5.6 and PostgreSQL
I'm relatively new to PHP. I'm sorting results on a page using a query string, like so:
if (isset($_GET['sort'])) {
$criteria = $_GET['sort'];
$result = get_all_sorted_restaurants_with_limit($limit, $criteria);
} else {
$result = get_all_restaurants_with_limit($limit);
}
where 'sort' is the url query string value. From doing a var_dump, I can see its correctly grabbing a string value.
Next, if the value is set, it calls the following method in my model:
function get_all_sorted_restaurants_with_limit($limit, $sort) {
$conn = open_database_connection();
$result = pg_query($conn, "SELECT * FROM restaurant r WHERE r.Type =
$sort LIMIT $limit;");
close_database_connection();
return $result;
}
Here's where things get wonky: When I try doing it with $sort, in the get_all_sorted_restaurants_with_limit method, it returns nothing. However, if I set it myself (i.e. to 'sports bar', like so:
$result = pg_query($conn, "SELECT * FROM restaurant r WHERE r.Type = 'sports bar' LIMIT $limit;");
it works. The var dump shows that this is the exact same string that's being grabbed from the query url.
How would I fix this? Do I need to cast it, or grab a strval() (I've tried, doesn't work.... ).
Looks like you're just missing the single quotes around $sort:
$result = pg_query($conn, "SELECT * FROM restaurant r WHERE r.Type = '$sort' LIMIT $limit;");
Seems you missed single quotes in your $sort variable but for to be sure try this,
function get_all_sorted_restaurants_with_limit($limit, $sort) {
$conn = open_database_connection();
$result = pg_query($conn, "SELECT * FROM restaurant r WHERE r.Type =
'$sort' LIMIT $limit;"); // see $sort variable
if($result)
{
//success, good to go
}
else
{
$error = pg_last_error($conn);
var_dump($error);
}
close_database_connection();
return $result;
}
I want t make a zend update query.
I was looking at zend documentation but I didn't see any example like the one I want.
The question its if may someone could help me with the query.
I want to make the next query:
Update cars set active = 0
Where id in (SELECT idCar FROM UserCars Where idUser=3)
Simplest way to run any complex query. There can be some better ways to do same.
$sql = "Update cars set active = 0
Where id in (SELECT idCar FROM UserCars Where idUser=3)";
$query = $this->getDbTable()->getAdapter()->query($sql, $data);
$query->execute();
Try this too
$data = array { 'active' = '0' };
$where = "id in (SELECT idCar FROM UserCars Where idUser=3)";
$db->update($data, $where);
Perhaps more Zendy way:
$idUser = 3;
$sub_select
= $db->select()
->from('UserCars', array('id'))
->where('idUser = ?', $idUser);
$updated_rows
= $db->update('cars',
array('active' => 0),
"id IN ($sub_select)"
);
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}%')");
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);