0

I am trying to get some relational database handling to work, using PHP and MySQL. To combine output from two different tables, I found some help here on stackoverflow. On of the suggestions was to combine the output in one result object (as I understand it), so the code looks like this:

// Function to read all projects from database
public function get_projects() {
    $query = "SELECT pt.*, at.* FROM ed_projects as pt, ed_project_address as at WHERE pt.project_id = at.project_id";
    $db_result = $this->db->query($query);
    $result_object = $db_result->result();
    return $result_object;
}

Where ed_project is the name of the main table, and ed_project_address is the subtable (or whatever it is called), which contains streetname, postal code, city, etc. for each project. The address properties are linked to projects by a project_id.

I can get this to work on the "reading from the database" part, but when I try to use it, I get an error: "Fatal error: Cannot use object of type stdClass as array in (...)". As I am using CodeIgniter, it is passed through 2 additional steps:

Project View Controller:

/* index() - Project view controller
 * Handles the showing of the project main page, that is the list of all
 * projects found in the database. See also views/projects/index.php.
 */
public function index() {
    $data['projects'] = $this->project_model->get_projects();
    $data['title'] = 'Ejendomme';
    $this->load->view('templates/header', $data);
    $this->load->view('projects/index', $data);
    $this->load->view('templates/footer');
}

Project View:

<?php foreach ($projects as $projects_item): ?>

    <h2><?php echo $projects_item['pt.projects']->project_name ?></h2>
    <div class="main"><?php echo $projects_item['at.address_street'] ?></div>

<?php endforeach ?>

The error is in Project View (index.php) on line 3. I have been reading quite a bit on objects in PHP, but the addition of the CodeIgniter framework seems to obscure things just enough for me to not get it :-(. Am I way off here? Or am I just missing the very last bit?

1
  • Try to print_r($projects); in your view. Commented Dec 8, 2014 at 4:58

1 Answer 1

1

you just made mistake at your model you returns the object

return $result_object;

but you using it as array at view. you should use it like this

  $projects_item->project_name

If you want to use it as array at your view you should return data from model like this

$result_object = $db_result->result_array();

Also your query will produce error if your both table has same column name. In that case you have to specify which column form which table you want to select.

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

2 Comments

Cool, that did the tricks, thank you very much! Slightly related bonus question though: If I call a column name which exists in both tables (like project_id), how do you control which of the columns it will show?
if you have same column name you cannot write select pt.*, at.*. You need to write all the columns name that you want selected. as example if you have project_name on both table.Then you can write pt.project_name, if you want project name from both table then you need to use AS. as example pt.project_name as project_name,at.project_name as project_name2. Hope you understand.

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.