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;
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 using CodeIgniter and I'm unable to get the where() selection method to work when using the results() method to fetch all the rows from a table.
Here's my model:
public function get_all_entries($id)
{
// Select row to be fetched
$this->db->where('id', $id);
$this->db->get('users');
// Execute the find query with provided data
$query = $this->db->get('users');
// Return an object with all the data
return $query->result();
}
It is supposed to return all the rows that match the $id argument in the users table, but instead, it simply fetches all the records in the table, including those that doesn't match the provided $id argument.
What I'm doing wrong here? I've tried the row() method and although it works with the where() it only returns a single row so it doesn't suit my case.
The problem is that you are calling the get() method twice, the first one with the where, but is not assign to a variable; the second one is assigned to a variable, but since the where clause is already used by the other one it gets everything. delete the first get and you should be fine.
public function get_all_entries($id)
{
// Select row to be fetched
$this->db->where('id', $id);
// Execute the find query with provided data
$query = $this->db->get('users');
// Return an object with all the data
return $query->result();
}
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
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.
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) {...}