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] );
Related
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
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'm trying to create pagination in codeigniter and I have it working, but I have a small issue. It seems to be loading all the entries in my database and not the selected ones I want.
public function original_count() {
$this->db->where('type', 'Original');
return $this->db->count_all("story_tbl");
}
I know that whats happening is that the last line is overrighting my previous statements. I can't seem to find a way around it though. I tried just a staight sql statement and then returning it, but I could not get that to work either.
this was my statement...
SELECT COUNT(*) FROM story_tbl where type = 'Original';
Some help would much appreciated! :)
CI has inbuilt count method
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:
https://www.codeigniter.com/userguide2/database/active_record.html
$total_count = $this->db->count_all_results('story_tbl', array('type' =>'Original'));
You could also use the built-in num_rows() function...
$query = $this->db->where('type', 'original')->get('story_tbl');
return $query->num_rows();
First Try this one.
$query = $this->db->where('tbl_field', 'value')
->get('your_table');
return $query->num_rows();
Besides this Codeigniter has it own function like the following.
$this->db->where('tbl_field', 'value')
->get('your_table');
return $this->db->count_all_results();
Or use this.
return $this->db->count_all('your_table');
Where wont work on count_all condition.. you can use below method to find out total number of rows..
public function count_all() {
$this->db->select ( 'COUNT(*) AS `numrows`' );
$this->db->where ( array (
'type' => 'Original'
) );
$query = $this->db->get ( 'story_tbl' );
return $query->row ()->numrows;
}
I am using Yii's Yii::app()->db->createCommand() to build an SQL query. In order to view the SQL code that Yii generates, I am using the getText() method of CDBCommand. Problem is, when I use the getText() method on SQL code that contain parameters, for example:
Yii::app()->db->createCommand()
->select("name")
->from('package')
->where('id=:id', array(':id'=>5))
->queryRow();
the getText() method returns the following SQL:
select name from package where id=:id
instead of:
select name from package where id=5
This is fine for simple queries, but for more complex queries with lots of parameters, it is quite a pain to copy/paste each parameter into the SQL code to test it.
Is there any way to display the parameters directly inside the SQL using getText() or some other method in Yii?
Cheers!
$sql = Yii::app()->db->createCommand()
->select("name")
->from('package')
->where('id=:id', array(':id'=>5))
->queryRow();
$query=str_replace(
array_keys($sql->params),
array_values($sql->params),
$sql->getText()
);
You can use the CDbConnetion::enableParamLogging propery. For instance, in config/main.php:
'db' => array (
'enableParamLogging' => true,
and the shown and logged error will contain the bound values.
why don you try as below.i am not an expert just posting to my knowledge if its no suitable for your prob pardon me...
$connection=Yii::app()->db;
$id=5; // you can able to change by "GET" or "POST" methods
$sql="SELECT name FROM package WHERE id = :id ";
$command = $connection->createCommand($sql);
$command->bindParam(":id",$id,PDO::PARAM_STR);
$dataReader=$command->query();
$rows=$dataReader->readAll();
$namevalue=array();
foreach($rows as $max)
{
$namevalue = $max['name'];
}
echo $namevalue; // which is the value u need
thank you...
Looks like you're after the PDOStatement that Yii is preparing:
$cmd = Yii::app()->createCommand();
$cmd->select("name")
->from('package')
->where('id=:id', array(':id'=>5))
->queryRow();
// returns a PDO Statement - http://php.net/manual/en/class.pdostatement.php
Yii::log($cmd->getPdoStatement()->queryString);
Does that work for you? Seems like it should (code untested).
I have a function that retrieves all tags from a table:
function global_popular_tags() {
$this->db->select('tags.*, COUNT(tags.id) AS count');
$this->db->from('tags');
$this->db->join('tags_to_work', 'tags.id = tags_to_work.tag_id');
$this->db->group_by('tags.id');
$this->db->order_by('count', 'desc');
$query = $this->db->get()->result_array();
return $query;
}
I have another table called 'work'. The 'work' table has a 'draft' column with values of either 1 or 0. I want the COUNT(tags.id) to take into account whether the work with the specific tag is in draft mode (1) or not.
Say there are 10 pieces of work tagged with, for example, 'design'. The COUNT will be 10. But 2 of these pieces of work are in draft mode, so the COUNT should really be 8. How do I manage this?
Try changing:
$this->db->from('tags');
$this->db->join('tags_to_work', 'tags.id = tags_to_work.tag_id');
To:
$this->db->from('tags, work');
$this->db->join('tags_to_work', 'tags.id=tags_to_work.tag_id AND work.id=tags_to_work.work_id');
And Adding:
$this->db->where('work.drafts', 0);
You can use pure sql instead of using the active record class, I myself are working with CI for over 2 years and most of the time I am avoiding the active record class, cause straight sql is much easier to debug and write complex queries.
This is how i would use it.
$sql = "SELECT...your sql here";
$q = $this->db->query($sql);
...
//Do something with your query here