Welcome Guest, Not a member yet? Register   Sign In
load database blank screen
#1

I get a blank screen when it tries to do load->database().  No error message.  Nothing in the log.  I verified that the database hostname, username, password and database are all correct using MySql Workbench.

I saw a post somewhere saying that CodeIgniter should not use PHP > 5.3.   Could that be it?  I have PHP 5.6.1 and CI 2.2

This is the model/news_model.php (from the news blog tutorial):
Code:
<?php
class News_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
    }
       public function get_news($slug = FALSE)
       {
               if ($slug === FALSE)
               {
                       $query = $this->db->get('news');
                       return $query->result_array();
               }

               $query = $this->db->get_where('news', array('slug' => $slug));
               return $query->row_array();
       }
}

This is config/database.php:
Code:
$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'my_password';
$db['default']['database'] = 'my_database';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
Reply
#2

Could you show the controller that uses the model?
Reply
#3

Thanks for the reply.  Here is some more code.

The controller, news.php:
Code:
class News extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('news_model');
    }

    public function index()
    {
        $data['news'] = $this->news_model->get_news();
        $data['title'] = 'News archive';
    
        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
    }

    public function view($slug)
    {
        $data['news_item'] = $this->news_model->get_news($slug);
    
        if (empty($data['news_item']))
        {
            show_404();
        }
    
        $data['title'] = $data['news_item']['title'];
    
        $this->load->view('templates/header', $data);
        $this->load->view('news/view', $data);
        $this->load->view('templates/footer');
    }
}

In routes.php:
Code:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
Reply
#4

You probably don't have the PHP mysql extension installed.
Reply
#5

(01-19-2015, 12:05 PM)Narf Wrote: You probably don't have the PHP mysql extension installed.

No that's not it. phpinfo() show mysql and I wrote a little PDO script to read from a table.
Reply
#6

You don't load the database from within a model. Do it in autoload (best since I bet most of your app uses db), or in the controller before loading/using the model.
Reply
#7

(This post was last modified: 01-22-2015, 01:45 PM by sneakyimp.)

(01-19-2015, 01:54 PM)treadman Wrote:
(01-19-2015, 12:05 PM)Narf Wrote: You probably don't have the PHP mysql extension installed.

No that's not it.  phpinfo() show mysql and I wrote a little PDO script to read from a table.

This may still be correct. the mysql extension has been deprecated and you should not use it. Try mysqli (stands for mysqli improved) instead. I can't seem to locate any documentation links that describe what all the valid options are. The CI 3 docs look fairly helpful.

EDIT: just to be clear, the old mysql extension may not exist in your new PHP version. You would still see some MySQL configuration information in phpinfo, but that would be referring to mysqli ("MySQL Improved") or perhaps PDO or something.
Reply
#8

Additionally, a PDO script may not tell you that the mysql extension is not installed, because PDO does not have to use the mysql extension in newer versions of PHP.
Reply
#9

try this simple test

Add this function to your controller
/**
*/
function test_db($terms){
if($terms){
$obj = $this->news_model->get_news($terms); // this should return array as per your model code
print_r($obj); //should show array output if the term is found in query
}
else{
print 'no terms param is passed on';
}
}
/* end test func*/

If you can see the output per 'terms' passed to controller then you have failed to iterate the array in your view, check your code

The testlink url should be in this format http://yournameserver/news/test_db/anyterm
OR http://yournameserver/index.php/news/test_db/anyterm
depends on how you set up your codeigniter site

Good luck
Reply
#10

(01-30-2015, 10:24 PM)dbui Wrote: try this simple test

Add this function to your controller
/**
*/
function test_db($terms){
    if($terms){
       $obj = $this->news_model->get_news($terms); // this should return array as per your model code
        print_r($obj);  //should show array output if the term is found in query
    }
    else{
          print 'no terms param is passed on';
    }    
}
/* end test func*/

If you can see the output per 'terms' passed to controller then you have failed to iterate the array in your view, check your code

The testlink url should be in this format  http://yournameserver/news/test_db/anyterm
OR http://yournameserver/index.php/news/test_db/anyterm  
depends on how you set up your codeigniter site

Good luck

Enable log error

or copy this to your index.php

if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
case 'development':
// Report all errors
//error_reporting(E_ALL);

// Display errors in output
//ini_set('display_errors', 1);
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
break;

case 'testing':
case 'production':
//error_reporting(0);
ini_set('display_errors', 1); // This will turn error reporting on
error_reporting(E_ALL);
break;

default:
exit('The application environment is not set correctly.');
}
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB