Please help, I am have error with codeigniter group_by and having count. This is my code:
$this->db->select('*, count(*) as jumlahnya')
->from('tb_taruhan')
->where($where)
->group_by('tebak')
->having(count('tebak'))
->order_by('jumlahnya','DESC');
$query = $this->db->get();
return $query;
The mySql HAVING clause requires 2 parameters: a "something" that needs to be evaluated (in your case 'COUNT(tebak)) and an evaluation (think of it as a filter)
Try
->having('COUNT(tebak)', '0');
The above is equivalent to HAVING COUNT(tebak)=0
CI allows two different syntaxes:
$this->db->having('user_id = 45');
$this->db->having('user_id', 45);
Both produce HAVING user_id = 45
->having('COUNT(`tebak`)', YOUR_NEED)
you called count function, not putting it inside string
Related
I thought it would be a good idea to define a query and use it for several selects or counts later, but it does not work. The second select has both wheres in the sql statement:
$query = Pic::where('pics.user_id',$user->id);
if($cat) $query->where('cat',$cat);
if($year) $query->where('jahrprod',$year);
$zb = $query->select('pics.id','pics.title','pics.created_at')
->where('pics.id', '>', $pic->id)
->orderBy('pics.id')
->take(2)
->get()->reverse();
$za = $query->select('pics.id','pics.title','pics.created_at')
->where('pics.id', '<', $pic->id)
->orderBy('pics.id')
->take(13)
->get();
Query:
SELECT `pics`.`id`, `pics`.`title`, `pics`.`created_at`
FROM `pics`
WHERE `pics`.`user_id` = '3'
AND `pics`.`id` > '2180'
AND `pics`.`id` < '2180'
ORDER BY `pics`.`id` ASC, `pics`.`id` ASC
LIMIT 13
I tried to "pass it as reference" i.e. &$query->select... but "only variables can be passed as reference".
How can I use the query , or save it, and use it for both actions. Is it possible?
You are updating object state with the statements when you do $query->where(), so yeah, when you're doing a second select, all conditions from the first one are still there. Thats the reason why these lines work without any assignments:
if($cat) $query->where('cat',$cat);
if($year) $query->where('jahrprod',$year);
To achieve described behaviour you would need to create an query object copy:
$query = Pic::where('pics.user_id',$user->id);
if($cat) $query->where('cat',$cat);
if($year) $query->where('jahrprod',$year);
$queryClone = clone $query;
$zb = $query->select('pics.id','pics.title','pics.created_at')
->where('pics.id', '>', $pic->id)
->orderBy('pics.id')
->take(2)
->get()->reverse();
$za = $queryClone->select('pics.id','pics.title','pics.created_at')
->where('pics.id', '<', $pic->id)
->orderBy('pics.id')
->take(13)
->get();
Notice that mere assignment would not work here:
$queryClone = $query;
Because that would pass object reference and would result in the same behaviour as in your case. Clone creates a full object copy.
http://php.net/manual/en/language.oop5.cloning.php
Apologies if the title is confusing. Currently I have this as part of a get_where function:
$query = $this->db->select('*');
$query = $this->db->get_where('Accounts', array('active' => 1));
return $this->result_array($query);
How can I add to the get_where() statement to also look in a specific column that contains a string value I tell it to look for? For example, I want it to also look in the column AccountKey and return all rows that contain the string '120-' in that column, in addition to active = 1.
You can use like for this, Your query would become something as follows:
$query = $this->db
->select('*')
->from('Accounts')
->where('active', 1)
->like('AccountKey', '120-', 'after')
->get();
In my Model, I wrote this function with MySQL raw query
function get_question_result($lr_id)
{
$raw_query = 'SELECT question_record.qr_id, LEFT(question.question, 50) as question,
question.correct_answer
FROM question_record
INNER JOIN question ON question.q_id = question_record.q_id
WHERE question_record.lr_id = '.$lr_id.' ';
$query = $this->db->query($raw_query);
$questionresult = $query->result_array();
return $questionresult;
}
It worked fine. It gave me the array I want. I continued my project.
Then suddenly I was curious to try it in CI Active Record Class.
function get_question_result($lr_id)
{
$this->db->select('question_record.qr_id, LEFT(question.question, 50) as question, question.correct_answer');
$this->db->from('question_record');
$this->db->join('question', 'question.q_id = question_record.q_id', 'inner');
$this->db->where('question_record.lr_id', $lr_id);
$result = $this->db->get()->result_array();
return $result;
}
It didn't work. It gave me this error
PHP Fatal error: Call to a member function result_array() on a non-object
Just out of curiosity, where did I do wrong?
Was it me writing it wrong or the result data structure with Active Record is just different?
'cause when I tried it again in Active Record without selecting this field
LEFT(question.question, 50) as question
It worked but it didn't give the field I want. Do you guys know why?
In your $this->db->select() call you need pass FALSE as second parameter so that active record will not try to add backticks ` for your columns in select statement
function get_question_result($lr_id)
{
$this->db->select('question_record.qr_id, LEFT(question.question, 50) as question, question.correct_answer',FALSE);
$this->db->from('question_record');
$this->db->join('question', 'question.q_id = question_record.q_id', 'inner');
$this->db->where('question_record.lr_id', $lr_id);
$result = $this->db->get()->result_array();
return $result;
}
According to docs
$this->db->select() accepts an optional second parameter. If you set
it to FALSE, CodeIgniter will not try to protect your field or table
names with backticks. This is useful if you need a compound select
statement.
$this->db->select();
I am trying to do the following query in Axon but can't make it work. Using normal query it works -
SELECT user_name, email_id FROM ors_email_user WHERE email_sent=false LIMIT 5;
In Axon, I try to do the following -
$users = new Axon('ors_email_user');
$users->load(array('email_sent=:email_sent', array(':email_sent' => false)), '', 3);
while(!$users->dry()) {
echo 'here';
}
It never goes inside the while loop. What is wrong with the query? Is there a way where I can see what query is actually being formed.
I believe the load function only allows 3 parameters to be passed, where the third is the columns that should be ordered.
You should use the find() or select() function. From the website:
find( [criteria],[order],[limit],[offset] );
select( fields,[criteria],[grouping],[order],[limit],[offset] );
I have a function which I use all over the place to pull users friends out of a database. However I recently had to delete a few users for causing problems on the forums, this has given me a few "Trying to get property of non-object" problems and I have traced it down to this function.
function isFriend($user_id, $friend_id){
$this->db->from('friends');
$this->db->where('user_id', $user_id);
$this->db->where('friend_id', $friend_id);
$this->db->where('removed', 0);
$query = $this->db->get();
$result = $query->result();
return count($result);
}
Does anyone know how I can adjust this function to ignore deleted users?
If all you're doing is counting the results you can try this instead:
$this->db->count_all_results();
Like this:
function isFriend($user_id, $friend_id)
{
$num = $this->db
->from('friends')
->where('user_id', $user_id)
->where('friend_id', $friend_id)
->where('removed', 0)
->from('TABLE_NAME')
->count_all_results();
return $num;
}
It should return 0 if there are no results, whereas $query in your current function (I believe) may not return an object you can call result() on (hence your error).
http://codeigniter.com/user_guide/database/active_record.html
$this->db->count_all_results();
Permits you to determine the number of rows in a particular Active
Record query. Queries will accept Active Record restrictors such as
where(), or_where(), like(), or_like(), etc. Example:
echo $this->db->count_all_results('my_table'); // Produces an integer, like 25
$this->db->like('title', 'match');
$this->db->from('my_table'); echo
$this->db->count_all_results(); // Produces an integer, like 17
There may be "better" ways, but without knowing the guts of your application it's hard to say.
why did you actualy remove them from database when you have the flag "removed" ?
Anyway, i think you should also remove the connections of those removed users, i don't see anything wrong with the function.
Trying to alter the function to work, it's just a hack and it's not good practice.
I think the function itself looks fine but it seem likely your errors are caused when the query returns 0 results.
if the removed col in the friends table or the users table?
Maybe you should perform a join to the users table.