0

I have read 1, 2 and several other threads prior to post this issue but none of them solved my problem. I am totally new with PHP and got stuck in attempting to learn MVC with codeigniter. The function which causing error is

public function get_results() 
{ 
    $libraries = array("", "JQuery", "MooTools", "YUI Library", "Glow"); 
    $table_rows = ''; 

    for ($i = 1; $i < 5; $i++) 
    {
         $this->db->select('COUNT(choice) choices_count')->from('js_libraries')->where('choice',$i);
         $result = $this->db->get();
         $table_rows .= "<tr><td>" . $libraries[$i] . " Got:</td><td><b>" . $result[0] . "</b> votes</td></tr>"; 
         
    } 
 } 

I have executed the query on phpmyadmin and it shows result (a single integer) but I am unable to make this CI_DB_mysql_result object work. I have tried all result(), result_array(), getResult() functions but it says undefined functions for this kind of object. How can I get result from CI_DB_mysql_result?

2
  • Add result_array method to get result as array. $result = $this->db->get()->result_array(); Commented Mar 12, 2021 at 11:33
  • I did this but it gave me Array to string conversion error on next line when getting results from $result[0] Commented Mar 12, 2021 at 11:50

1 Answer 1

1

Ok. Thats because result_array returns 2D array which contains multiple rows from table. In this case you want only one row so you can use either of the following:

  1. row() method which returns result as object. $result = $this->db->get()->row(); Then $result->choices_count to get count value.

  2. row_array() method which returns result as array. $result = $this->db->get()->row_array(); Then $result['choices_count'] to get count value.

  3. unbuffered_row() method which returns result as object $result = $this->db->get()->unbuffered_row(); Then $result->choices_count to get count value.

  4. unbuffered_row('array') method which returns result as array. $result = $this->db->get()->unbuffered_row('array'); Then $result['choices_count'] to get count value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.