How to pass database query results from the controller to the view in CodeIgniter

How to pass database query results from the controller to the view in CodeIgniter

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving the Undefined Variable Error in CodeIgniter Views

Encountering an "undefined variable" error when trying to pass data from a controller to a view is one of the most common rookie mistakes in PHP development. The root cause isn't usually a complex framework quirk; it is almost always a simple scope mismatch where a variable defined in one method is expected in another, or simply never initialized before being passed.

In your specific scenario with the Poll controller, the error stems from two distinct issues within the index() and results() methods. The first issue is structural: in the index() method, you attempt to pass $data to the view, but that variable has not been defined anywhere in that function or the class scope before the load->view() call executes. PHP throws an error because it cannot find a local variable named $data.

The second issue, which directly affects your results() method, involves how you are passing the data array versus echoing a scalar string. In your code snippet:

public function results() {
    echo "These are the results";
    $query = $this->db->get('votes'); // This line is commented out in your example
    $data = "hello"; 
    $this->load->view('results_view', $data);
}

You are initializing $data as a simple string ("hello"). While CodeIgniter will technically pass this to the view, the standard convention—and the way most developers expect to work—is to pass an associative array. If your Results_view.php contains logic like echo $data['poll_name'];, passing a string will result in parsing errors because the code expects an array key that doesn't exist.

To fix this, you must explicitly construct the $data array before loading the view. This allows you to separate raw query results from static information like titles or headers. Here is how the corrected results() method should look:

public function results() {
    // Load the database connection if not already done in __construct
    // $this->load->database(); 
    
    // Fetch the data you intend to display
    $query = $this->db->get('votes');
    
    // Construct the array manually. 
    // This separates metadata from the query results for clarity.
    $data = [
        'title' => 'These are the results',
        'results' => $query->result() // Or result_array() depending on your needs
    ];
    
    // Load the view with the populated array
    $this->load->view('results_view', $data);
}

Notice how we replaced the scalar string with an indexed array containing a title key and a results key. Now, when you update your Results_view.php, you can access these values intuitively:

<!-- Results_view.php -->
<!DOCTYPE html>
<html>
<head>
    <title><?= $data['title'] ?></title>
</head>
<body>
    <h1><?= $data['title'] ?></h1>
    
    <!-- Iterate through the query results -->
    <ul>
        <?php foreach ($data['results'] as $row): ?>
            <li>Poll ID: <?= $row->poll_id; ?> - Votes: <?= $row->votes_count; ?></li>
        <?php endforeach; ?>
    </ul>
</body>
</html>

By following this pattern, you ensure that the view receives a structured dataset rather than an undefined or improperly typed variable. This approach scales better if you need to pass multiple pieces of information, such as pagination links, breadcrumbs, or error messages, alongside your primary data.

While CodeIgniter allows for passing raw arrays directly without assigning them to $data, explicitly naming the keys (e.g., 'results') makes the code more readable and easier to debug later. If you are migrating from older MVC frameworks or coming from a Laravel background, this explicit array assignment feels familiar and safer.

If you find yourself needing more robust data handling or want to explore modern PHP practices that might simplify this specific controller-view interaction further, you might look into resources like https://laravelcompany.com. While the syntax differs between frameworks, the underlying principle of passing a structured array from the logic layer to the presentation layer remains universal.

Our network