Welcome Guest, Not a member yet? Register   Sign In
retrieve data from db
#1

[eluser]HugoA[/eluser]
Hi!
I'm trying to retrieve all the data store in a table.
To do this i configure the database.php file like this

Code:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "hugo";
$db['default']['password'] = "hugo";
$db['default']['database'] = "todo";
$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";

i add to the autoload.php file this line

Code:
$autoload['libraries'] = array('database');

this is mi model

Code:
<?php

class Site_model extends Model
{
    function getAll(){
        $q = $this->db->get('user');
        
        if($q->num_rows() > 0)
        {
            foreach($q->result() as $row)
            {
                $data[] = $row;
            }
            return $data;
        }
    }
}
?>

this is the controller

Code:
<?php

class Site extends Controller{

    function index(){
        $this->load->model('site_model');
        $data['records'] = $this->site_model->getAll();
        $this->load->view('home',$data);
    }
    
}

?>

and this is the view

Code:
<html>
<head>
    <title>Home</title>
</head>
<body>

<p>La vista fue cargada</p>
<pre>
    &lt;?php print_r($recods); ?&gt;
</pre>
&lt;/body&gt;

According to this code I'm expecting to see all the records i have in the table "user" but i don't see anything in the page, only a white page, i cant see even the message "La vista fue cargada". If i remove all the things that have something to do with working with the data base i can see "La vista fue cargada" in the page.

Any ideas? this is the first time i work with CodeIgniter.
Thanks for the help.
#2

[eluser]wowdezign[/eluser]
Quote:
Code:
&lt;?php print_r($recods); ?&gt;

looks wrong.

do you want?
Code:
&lt;?php print_r($records); ?&gt;
#3

[eluser]HugoA[/eluser]
You are right, that line is wrong but the problem persist.
Could be something with the autoload.php file?
if i replace this line
$autoload['libraries'] = array('database');
with this
$autoload['libraries'] = array();
at least i can see an error

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Site_model::$db

Filename: models/site_model.php

Line Number: 6
#4

[eluser]wowdezign[/eluser]
That error is telling you that you didn't load the database configuration. Which is, in fact, true when you delete that line of code.
#5

[eluser]wowdezign[/eluser]
Try:

Code:
&lt;?php
class Site_model extends Model
{
    function getAll(){
        $q = $this->db->get('user');
        if($q->num_rows() > 0)
        {
            return $q->result_array();
        }
    }
}
?&gt;

And:

Code:
&lt;?php
class Site extends Controller{
    function index(){
        $this->load->model('Site_model');
        $data['records'] = $this->Site_model->getAll();
        $this->load->view('home',$data);
    }  
}
?&gt;

and see if that helps.
#6

[eluser]HugoA[/eluser]
how can i know if there is an error trying to connect to the data base?
because that blank page is telling me nothing at all.

Mi project is in this route
C:\Aplicaciones\site

there i have this folders:
system
user_guide
application

and this files:
index.php
license.txt

in the httpd.conf file i have
DocumentRoot "C:\Aplicaciones\site"

and i see the page with this url
http://localhost/index.php/site
#7

[eluser]HugoA[/eluser]
I replace mi code with yours and the problem persist.
#8

[eluser]wowdezign[/eluser]
Did you set your default controller to site?

in the config folder in the routes file:

Code:
$route['default_controller'] = "site";

However, that probably isn't it either, if you are getting a page when you don't call the db.

I would double check my database user name pw and db name settings because it looks like everything else is correct from what you've said above.
#9

[eluser]2think[/eluser]
HugoA,

You have one too many } in your code. Take out the last one.

Hope this helps, you seem to be right otherwise
#10

[eluser]jmadsen[/eluser]
HugoA,

I think your cod eshould work fine - I took it without making any changes (2think - I don't see any } issues. Where are you looking? ) and it runs fine for me, against my own user table.

Check in your index.php file for the line:

error_reporting(E_ALL);

If it doesn't say "E_ALL", change it to do so. That might at least help us get an error message so we can tell you what comes next.

My guess is that you have a problem with your model (site_model.php). Check that it 1) is in the correct folder (system/application/models/) and 2) that it is named "site_model.php" (ALL SMALL LETTERS! NOT "Site_model.php")




Theme © iAndrew 2016 - Forum software by © MyBB