When I try to run the follow query it never returns anything, it just keeps running. What am I doing wrong with it?
$this->db->distinct('customer.Name')
->from('customer')
->from('invoicelinedetail')
->from('invoice')
->from('iteminventory')
->where('invoice.CustomerRef_ListID = customer.ListID')
->where('invoicelinedetail.IDKEY = invoice.TxnID')
->where('invoicelinedetail.ItemRef_ListID =', $id)
->order_by('customer.Name', 'desc');
$query = $this->db->get();
After $this->db->get(); you need to retrieve your results
More info from CI Docs
To do this, set $query= to one of the following:
$this->db->result_array(); //returns array
$this->db->row_array(); //returns single row as array
$this->db->result(); //returns object
$this->db->row(); //returns single row as object
Related
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 write a CSV in Codeignter for a result set fetched from db.
I am already using the solution mentioned here. Reports in Codeigniter
But the issue is that it writes the whole table. Now I want to fetch and write specific records. So here is my model code.
$where = "hangtag_spoc_number = 2202";
$result = $this->db->select('*')
->from('hangtag_request')
->where($where)
->get()
->result_array();
return $result;
It gives me this error.
You must submit a valid result object
If I change the model code to this
return $query = $this->db->get('hangtag_request');
It works perfectly. Is there any way I can make my model code to return the results in the form of DB object? Coz it seems thats the form we need.
Thanks.
If you need result object then you shouldn't use the result_array()
$this->db->get();
The above statement runs the selection query and returns the result.It does return in form that can be used for presentation.
result_array()
This function returns the query result as an array, or an empty array when no result is produced.
Its good to use return $query = $this->db->get('hangtag_request'); for obtaining result object.
I got it solved.
Just had to remove result_array() so the correct code becomes
$where = "hangtag_spoc_number = 2202";
$result = $this->db->select('*')
->from('hangtag_request')
->where($where)
->get();
return $result;
I am trying to do this sql query on codeigniter
SELECT* FROM person WHERE type = 'staff' AND description LIKE 'university%'
I am not sure if it is correct...
$this->db->get_where('person' , array('type'=>'staff'))
->like('description','university%')
->result_array();
does anyone have an idea about my case? thanks in advance ...
Using Active Record you can do so
$query = $this->db->select('*')
->from('person')
->where('type','staff')
->like('description','university','after')
->get();
$result = $query->result_array();
Make sure you pass after as a third parameter in like() function so active record will add the wild card i.e % after university so it will look like LIKE 'university%'
The docs has this explained very well, but their API is very simple. To produce the query you need the relevant code which looks like this:
$this->db->select('*')
->from('person');
->where('type', 'staff')
->like('description','university');
$query = $this->db->get();
$result = $query->result_array();
I never used chaining(even though i know it's possible), but breaking down your question should be easy ;
$this->db->from('person');
$this->db->where('type','staff');
$this->db->where('description', 'university%');
$query = $this->db->get();
$result = $query->result_array();
return $result;
I am using Eloquent ORM outside of Laravel-4 and I am building a custom Paginator.
First, I build a query using Fluent Query Builder. I want to get the number of result the query could return using count() and then I do a custom pagination using take(x) and skip(y). I need to do the count() before the take()->skip()->get() so I dont fall outside of the page range. The problem is that when I use the count() method on the query, it seems to remove any select I added previously.
I isolated the problem to this simple example:
$query = DB::table('companies')
->join('countries','companies.country_id','=','countries.id')
->select(
'companies.name as company_name',
'countries.name as country_name'
);
$nbPages = $query->count();
$results = $query->get();
//$results contains all fields of both tables 'companies' and 'countries'
If i invert the order of the count and get, it works fine:
$results = $query->get();
$nbPages = $query->count();
//$results contains only 'company_name' and 'country_name'
Question: is there a more elegant way the using something like this:
$tmp = clone $query;
$nbPages = $tmp->count();
$results = $query->get();
There is not, unfortunately. Open issue on github about the problem: https://github.com/laravel/framework/pull/3416
I have a query which im trying to return the exact entry for, but instead it keeps returning the 'Array' ... how do i extract just the one 'name' in looking for?
My current query looks like so
$query = $this->db->query('SELECT name FROM '.$this->table_name.' WHERE id =1');
return $query->result();
Basically, i just want to return the actual name, not an array
You need to dig into the objects returned by the query. row() returns the first row in a result set:
$row = $query->row();
return $row->name;
result() returns an array of objects representing the rows in the result set. You need to get the object itself, then get it's 'name' property.
It's also worth checking $query->num_rows() to make sure you have results:
if ($query->num_rows() > 0) {...}