Welcome Guest, Not a member yet? Register   Sign In
Problem with News Section Tutorial
#1

[eluser]duartix[/eluser]
(EDIT) Solved.

Cheers.

I'm totally new to both PHP and CI and while trying to get past the News Section Tutorial, in the last line where it tells me to load the News view (in my case: http://localhost/testeCIstatic/index.php/news), I'm getting an unexpected error:
( ! ) Fatal error: Call to a member function row_array() on a non-object in C:\xampp\htdocs\testeCIstatic\application\models\news_model.php on line 16

This is my model:

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();
    }

}

There is one strange issue in that the error reporting says it borks on line 16 but while debugging with Netbeans and Xdebug, I see that it happens well before that, as it gets inside the IF statement and it crashes in the
Code:
return $query->result_array();
statement while trying to make an array from $query.

$query is boolean and holds 0, so I'm guessing I've never managed to travel to the database. Sad

So, I've decided to debug:
Code:
public function __construct() {
        $this->load->database();
    }

and into Loader.php/public function database

Code:
public function database($params = '', $return = FALSE, $active_record = NULL)
{
  // Grab the super object
  $CI =& get_instance();

  // Do we even need to load the database class?
  if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db))
  {
   return FALSE;
  }

  require_once(BASEPATH.'database/DB.php');

  if ($return === TRUE)
  {
   return DB($params, $active_record);
  }

  // Initialize the db variable.  Needed to prevent
  // reference errors with some configurations
  $CI->db = '';

  // Load the DB class
  $CI->db =& DB($params, $active_record);
}

$CI gets properly set,
the if(class_exists ... statement doesn't return FALSE,
the require_once doesn't change $return from FALSE, so
the next if statement doesn't process,
CI->db seems to read my configuration properly from application/config/database.php which looks like this:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$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;

Yet $query seemed empty, so can anyone please give a lead on this?

Thanks in advance...
Duarte Bruno
#2

[eluser]duartix[/eluser]
Well, this is embarasing, a coworker just stepped in looked at my database configuration file and immediately spotted the problem:

Code:
$db['default']['database'] = "database_name";

It needed to be set. In my case the database was named data.

All is well that ends well.

But now, I'm questioning myself on where should lie the code for this kind of error trapping...




Theme © iAndrew 2016 - Forum software by © MyBB