Welcome Guest, Not a member yet? Register   Sign In
connecting and retrieving database data
#1

[eluser]Unknown[/eluser]
hey everyone.i still can't get through in connecting and retrieving data from my database.
1)in my database.php config file,i configured the db_name to my own name,username set to root and active_group allowed as default (for now).
2)in the welcome.php controller file i loaded the db with: $this->load->database('default');
3)finally in my view page where i wish to display the db data,i typed:

<?php
$query =$this->db->query("YOUR QUERY");
if($query->num_rows()>0)
foreach ($query->result() as $row){
echo $row->title;
echo $row->date;
echo $row->author;
echo $row->content;
}

Testing it i recieved the following error: FATAL ERROR: Call to a member function on a non-object in c:\program files\easyphp1-8\www\codeigniter_1.7.2\system\application\views\content.php on line 161

Please i will be very greatful if someone can help me figure out my error.Thanks for the assistance given me so far.
#2

[eluser]verynewtothis[/eluser]
Try changing your 'load database' call to
Code:
$this->load->database();
#3

[eluser]iConTM[/eluser]
If I can add some comments about good practice? Wink

I would follow the MVC pattern:

model

Code:
function get ()
{
  $query = $this->db->get('mytable');
  return $query->result();
}

controller

Code:
$this->load->database();
$this->load->model('my_model');

$rows = $this->my_model->get();

// do some conversion here (if needed)
$data['rows'] = $rows

$this->load->view('content', $data);

view

Code:
foreach ($rows as $row)
{
echo $row->id;
//...
}

Did it work out with the $this->load->database()?

greetings
#4

[eluser]Juan Velandia[/eluser]
Please check this thread, It uses mvc structure. The tables names are different but it's easy (I think) to figure it out

http://ellislab.com/forums/viewthread/149458/

hope it helps




Theme © iAndrew 2016 - Forum software by © MyBB