9

I'm trying to get a database query which is an object converted to an associative array, so that I can use it in the calendar class in codeigniter.

This is my model:

<?php

class Get_diary_model extends Model {
    
    function getAllDiaries($year,$month) {
        
        $data = $this->db->query("SELECT day AND entry FROM diary WHERE month=$month AND year=$year"); // the entries for the relevant month and year
        
        foreach($data->result_array() as $row) { // return result as assoc array to use in calendar
            echo $row['day'];
            echo $row['entry'];
        }

        return $data;
        }
    }

and this is the error I get:

Fatal error: Cannot use object of type CI_DB_mysql_result as array in C:\wamp\www\mm\system\libraries\Calendar.php on line 219
2
  • This is a typo question. Your query's SELECT is asking for day AND entry, but it should be day, entry. This should be return $this->select('day, entry')->get_where('diary', ['month' => $month, 'year' => $year])->result_array(); Commented Nov 5, 2023 at 22:51
  • Given the question's title, it is unclear what your desired result is. Commented Nov 5, 2023 at 23:43

4 Answers 4

6

Check ou this video tutorial, it will help you -> http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-the-calendar-library/

Your model should look like this:

    function getAllDiaries($year,$month)
    {
        $q = $this->db->query("SELECT day AND entry FROM diary WHERE month=$month AND year=$year");

        if($q->num_rows() > 0):
            foreach($q->result() as $row):
                $data[] = $row;
            endforeach;
            return $data;
        else:
            return false;
        endif;
    }

and your controller:

    function index($year = null, $month = null)
    {
        $this->load->model('Get_diary_model');

        if (!$year) {
            $year = date('Y');
        }
        if (!$month) {
            $month = date('m');
        }

        $data['calendar'] = $this->Get_diary_model->getAllDiaries($year, $month);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect! Thank you very much, my controller is similar to that, I just couldn't figure out the model part. Will check out the video as well...
I'm giving downvote because of obvious SQL injection... It might solve the problem (stated in question), but the code doesn't care about any escaping at all, therefore it might bring more problems.
You are of course correct about the sql injection but that was not initially the problem and therefore the downvote is a bit unfair. If I had not answered the question correctly I would understand.
3

The problem was not in your use of result_array(), more that you later return $data directly. $query = $this->db->query() then use $query->result_array() in the foreach. Then you can return $data after building it up in the foreach.

The other answer is a long-winded way of writing the following:

function getAllDiaries($year,$month)
{
    $sql = "SELECT day AND entry FROM diary WHERE month=$month AND year=$year";
    return $this->db->query($sql)->result();
}

But of course that will return an array of objects, not an multidimensional array.

2 Comments

Hi Phil thanks for the reply, I gave that a try but it doesn't quite do what I want. I'm trying to get the 'day' field from the db to be the array key, and the 'entry' the value. So far, no luck.
var_dump() is the answer here. Whenever you get an unexpected result, var_dump(), see what is there and act accordingly. To get an assoc array you are using the correct syntax. It is something in your foreach or array manipulation later that is going wrong.
1

Use below simple method,

$query = $this->db->get_where('table', array('table_id' => $id));

        $queryArr = $query->result();        

        foreach ($queryArr[0] as $key=>$val)
        {
            $row[$key]=$val;
        }

print_r($row); //this will give associative array

1 Comment

It makes no sense to collect the result set data with result(), then use a foreach() to iterate the first row (which might not exist), then populate a new variable ($row) which is identical to $queryArr[0]. This is a not-well-thought-out answer. Just use row_array()
0

Here is the solution for CodeIgniter-3

function getAllDiaries(){
    $query = $this->db->query("YOUR QUERY HERE");
    return $query->result('array');
}

OR

function getAllDiaries(){
    return $this->db->query("YOUR QUERY HERE")->result('array');
}

Note: result() function accept "array" or "object" as parameter. Default is "object"

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.