This is not working in my CI app:
$query = $this->db->select_max('order')->get('posts');
print_r($query);
Why is that?
I have a column in my DB called order (int, where the highest value is currently 6) and the table is called posts
Why nothing is outputted instead a number 6 ?
This just runs the query, you need to use ->row() to get the result from it.
$this->db->select_max('order', 'max_order');
$query = $this->db->get('posts');
echo $query->row()->max_order;
DOCS:
https://www.codeigniter.com/userguide2/database/active_record.html
https://www.codeigniter.com/userguide2/database/results.html
Related
I want to fetch selected fields from my tables in laravel for that I assign my table fields in a variable. But always it gives me some database query error, here I added my code for that. This working fine when I use direct fields instead of them assign into a variable
$selectedfields = "'table1.*', 'table2.*','table3.column'"
$data = DB::table('table1')
->select($selectedfields)->get();
You can use DB:raw:
$selectedfields = 'table1.*, table2.*, table3.column';
$data = DB::table('table1')
->select(DB::raw($selectedfields))->get();
You might need to join table as well.
your query should be like this
$data = DB::table('table1')
->join('table2','table2.id','=','table1.id')
->join('table3','table3.id','=','table1.id')
->select('table1.*','table2.*','table3.*')->get();
I know this question has been posted already before Update with limit 1 in codeigniter use active record
But it seems combining UPDATE and LIMIT using Active Record doesnt work on my side. It will still update all record based on the WHERE clause. I'm currently using CodeIgniter 3.0.6. Is this a bug from CodeIgniter already?
screenshot of the query
You cannot directly add limit in update query. I get an alternative way. Firstly you get one row by where query and then run the update query with using fetching row unique key like unique auto increment id.
$singleInfo = $this->db->get_where($tbl, array('user_id'=>1));
$this->db->update($tbl,array('username'=>'FredSmith'),array('user_id'=>$singleInfo->id));
Well I fired up CI 3.0.6 and used this as a demonstration, as we cannot see your setup.
public function db_update_test_with_limit(){
$tbl = 'users';
// Use Chaining - Works
$this->db
->where('user_id',1)
->limit(1)
->update($tbl,array('username'=>'FredSmith'));
echo $this->db->last_query();
// Use discrete calls - Works
$this->db->where('user_id',1);
$this->db->limit(1);
$this->db->update($tbl,array('username'=>'FredSmith'));
echo '<br>';
echo $this->db->last_query();
}
And the results for both are...
UPDATE `users` SET `username` = 'FredSmith' WHERE `user_id` = 1 LIMIT 1
UPDATE `users` SET `username` = 'FredSmith' WHERE `user_id` = 1 LIMIT 1
So if you are doing the same thing and getting different results, then something funky is going on.
If you use this as a test and massage it to suit your situation, does it work or not?
$sqLines = SqLines::whereDocumentId($id)->get();
$sqLines->where('item_id','=',0)->count();
item_id is an unsigned integer field
In my development server, it shows result but in my production server with the record exists.
Any reason it happen?
You can use the following
$count = SqLines::where('document_id','=',$id)
->where('item_id','=',0)
->count();
OR
$data = SqLines::where('document_id','=',$id)
->where('item_id','=',0)
->get();
$count = count($data);
With get(), you retrieve the result, so you can't limit it after. Change your code to this:
$sqLines = SqLines::whereDocumentId($id)
->where('item_id','=',0)
->get();
echo $sqlLines->count(); // wil return the number of selected records.
Maybe you will need to change your code. Use one of the following options:
Option 1. Remove the "->get()".
Option 2. Remove the "->count()" and add a loop for check manually the result with a "foreach", "while", etc...
I'm new to smarty and prestashop. I'm building a quick, dirty module that pulls out cms pages with a particular category:
$result = Db::getInstance()->executeS('SELECT *
FROM ps_cms_lang
INNER JOIN ps_cms ON ps_cms_lang.id_cms = ps_cms.id_cms
WHERE ps_cms.id_cms_category =2
AND id_lang =1
LIMIT 0 , 30');
$smarty->assign('news', $result);
So far this is all working dandy. Thing is I want to format some of this data before I assign it to the template variable (news). How do I do this? 6 fields are returned in the $result variable. How do I get at them and do what I need (which is essentially just truncating some of the text in the description field that is returned) to do and then package them back up for the assign?
You may use $result as array, for example: $result['id_cms'].
What I want to do is select items from my database where the average review is greater than 7.
$this->db->select_avg('reviews.overall');
That query selects the average review 'as overall'.
Therefore i assumed i could simply then use
$this->db->where('overall>','7');
This however does not work.
ANy ideas?
Thanks
EDIT
Putting in a space
$this->db->select_avg('reviews.overall');
$this->db->where('overall >','7');
Yields the error
Column 'overall' in where clause is ambiguous
It is ambiguous but how am I now meant to reference it??
Thx
It's writen in the codeigniter user manual too:
$this->db->select_avg(); Writes a "SELECT AVG(field)" portion for your query. As with select_max(), You can optionally include a second parameter to rename the resulting field.
If you enable profiler in CI, you would see, what query does this code generate.
Your code is generated like this:
SELECT AVG(reviews.overall) as reviews.overall FROM ....
Use it like this:
$this->db->select_avg('reviews','overall');
$this->db->from('table name');
$this->db->where('overal >', 7);
$Q = $this->db->get();