Welcome Guest, Not a member yet? Register   Sign In
Database issue driving me insane.
#1

[eluser]jord[/eluser]
I've been playing a with ci for a new project and I'm really liking it so far, but I'm having a problem any time I try to connect to a database.

controller: books.php
Code:
<?php
class Books extends Controller
{
  function Books()
  {
parent::Controller();
  }
  function index()
  {
  $this->load->model('booksmodel');
  $qry = $this->booksmodel->get_entries();

  echo $qry->title;

}

?>

model: booksmodel.php
Code:
<?php

class Booksmodel extends Model
{

   function Booksmodel()
{
  parent::Model();
}

function get_entries()
{
  $query = $this->db->get('BK_books');
  return $query->result();
}
  
}

?>

When I load the books controller in a browser, I get a blank page, with no source.

If I comment out the foreach loop in the controller, then my unordered list tags show up in the source.

Can anyone tell me what I'm doing wrong?

-- Edited
Updated the code samples to my current code.
#2

[eluser]coolfactor[/eluser]
You're using $query->result() twice... once in the model and then again in the controller.
#3

[eluser]jord[/eluser]
Fixed the 2 result()s issue, but the code still isn't working.

I removed the result() from the controller, but now I get a php error: "Trying to get property of non-object".

There is data in the database, and a quick test file shows me that I can connect and read from the database without any trouble using mysql_* functions.

Is there perhaps something the ci active record code that my host just doesn't like?
#4

[eluser]coolfactor[/eluser]
Please post your revised code, or update your first post to reflect the changes.
#5

[eluser]batteries[/eluser]
edit: nevermind my question..

are you loading the database class?
#6

[eluser]coolfactor[/eluser]
jord,

I see you updated the code in your first post, but you're still using the query result incorrectly. Try reading the User Guide a bit more and use debugging techniques, such as var_dump($variable) to see what different variables contain. Those are invaluable skills to have when working with any programming language. Frameworks aren't meant to replace the understanding of the underlying language, merely make it easier to work with.
#7

[eluser]jord[/eluser]
All right, I'll give the user guide another go and play with debugging it tomorrow. I can do what I want to do in straight php without any difficulties, I'm just starting to look at some frameworks to take some of the tedium out of it.

In any case, it can wait 'til another day.




Theme © iAndrew 2016 - Forum software by © MyBB