4

I have the following code in helloworld.php:

<?php

class Helloworld extends CI_Controller {

public function __construct()
{
    parent::__construct();
}

public function index()
{
    $this->load->model("helloworld_model");
    $data["result"] = $this->Helloworld_model->getData();
    $data["page_title"] = "CI Helloworld appis";
    $this->load->view("helloworld_view", $data);
}

}

?>

The code stops executing after calling the parent constructor, without giving absolutely any error messages. Nothing appears in /var/log/apache2/error.log either. If I echo something before constructor call, it is echoed. If I type gibberish before the constructor call, a proper error message is printed. Why is this happening?

The site is running on Ubuntu server 12.04 with Code Igniter 2.1.4. and PHP 5.3.

Other files are helloworld_model.php:

<?php

class Helloworld_model extends CI_Model {

public function __construct()
{
    parent::__construct();
    $this->load->database();
}

public function getData()
{
    $query = $this->db->get("data");

    if ($query->num_rows() > 0)
    {
        return $query->row_array();
    }
    else
    {
        show_error("Database is empty");
    }
}

}

?>

And helloworld_view.php:

<html>
<head>
    <title><?php echo $page_title ?></title>
</head>

<body>
    <?php foreach($result as $row): ?>
        <h3><?php echo $row["title"]?></h3>
        <p><?php echo $row["text"]?></p>
        <br />
    <?php endforeach ?>

</body>
</html>

As far as I understand, the Controller constructor is what gets called absolutely first, so I don't think the rest of the files matter at this stage(?).

6
  • Please show the rest of the code. What happens when you do parent::__construct(); echo 'Hello';? Also do you have error reporting enabled and display_errors on? Commented Sep 9, 2013 at 23:06
  • Nothing gets printed after the parent constructor call. display_errors is on, and all other errors are printed normally. One moment, I'll post rest of my code. Commented Sep 9, 2013 at 23:13
  • What happens when you remove the constructor all together? I noticed your closing your php tags at the bottom of the page, try removing the closing php tag and any whitespace from the bottom of the controller. Commented Sep 10, 2013 at 1:27
  • @Jeemusu Absolutely nothing will appear on the page if I remove the constructor and nothing gets printed in other functions either. Removing the closing tags results in a parse error. Removing white space does nothing. Commented Sep 10, 2013 at 10:36
  • I started doing this following a really crappy tutorial with lots of mistakes and for an older version of CI, but I tried to fix it along the way to work with 2.1.4. I think I'm just going to start over with a more up to date tutorial since I just don't understand what can be wrong in this. Commented Sep 10, 2013 at 10:39

2 Answers 2

2

I had same problem that was resolved by changing 'dbdriver' => 'mysqli' to 'dbdriver' => 'mysql' in you config/database.php. Also make sure your database connection parameters are correct.

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

2 Comments

for php56, i change from mysql to mysqli.
I changed it from 'mysql' to 'mysqli' and issue is fixed.
1

My guess, and I would have to see your configs to be sure, is that there is a problem in the initialization of your Loader. Most commonly this has to do with the automatically loaded libraries, and sometimes it has to do with a bad database configuration. My first suggestion would be to try to get something working using the default configs. If that works, then you have a good starting point.

2 Comments

Indeed, when I started fresh, I encountered the same problem, but now it happens specifically only when I load the database inside a model. I think I am following instructions to the letter, and again it doesn't help that it doesn't give any error messages from this and only from this..
Well I still don't know what caused the original problem, but this db-related problem was solved by installing php5-mysql package.

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.