Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Database: When use database library, page is not visible
#1

[eluser]Lorin[/eluser]
Hi guys, I’m new in CodeIgniter. Please be indulgent.

So, I’m trying to write simple script to write user’s data from database to web page. My code is based on your second video tutorial.

I’ve got two scripts: users.php in controllers and users_view.php in views directory.

users.php:
Code:
<?php
class Users extends CI_Controller {
    function index() {
        $data['title'] = 'P - Seznam uživatelů'; // Page title (in <title> tags)
        $data['header'] = 'Seznam uživatelů'; // Header title (in <h1> tags)
        $data['query'] = $this->db->get('users'); // Query for data from database.
        
        $this->load->view('users_view', $data); // Loading view file
    }
}

?&gt;

users_view.php:
Code:
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;? echo $title; ?&gt;&lt;/title&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <h1>&lt;? echo $header; ?&gt;&lt;/title&gt;
    <table>
        <tr><td>Jméno</td><td>Heslo</td><td>E-mail</td><td>Aktivován</td><td>Akce</td></tr>
        &lt;?php foreach($query->result() as $row) { ?&gt;
            <tr><td>&lt;? echo $row->nick; ?&gt;</td> <td>&lt;? echo$row->passwd; ?&gt;</td> <td>&lt;? echo $row->email; ?&gt;</td> <td>&lt;?php if ($row->active == 0 ) { echo 'NE'; } else { echo 'ANO'; }?&gt;</td> <td>AKCE</td></tr>
        &lt;?php } ?&gt;
    </table>
    &lt;/body&gt;
&lt;/html&gt;

In my configuration files, I have this:

database.php:
Code:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = '******';
$db['default']['database'] = 'codeigniter';
$db['default']['dbdriver'] = ';
...

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

routes:
Code:
...
$route['default_controller'] = "users";
...

And in table 'users' within 'codeigniter' database:
Code:
mysql> SELECT * FROM users;
+----+------+--------------------+---------+--------+------------+------------+--------+
| id | nick | email              | passwd  | active | reg_date   | act_key    | rights |
+----+------+--------------------+---------+--------+------------+------------+--------+
|  1 | test | [email protected]       | test123 |      0 | 0000-00-00 | 123456789a |      0 |
|  2 | test | [email protected]       | test123 |      0 | 2010-11-01 | 123456789a |      0 |
|  3 | pepa | [email protected] | papa    |      0 | 2011-05-04 | 5782125    |      0 |
+----+------+--------------------+---------+--------+------------+------------+--------+

I think, everything seems to be ok. The problem started in browser. I got only white page with no content. Sad

Thanks for help, Lorin.
#2

[eluser]jvicab[/eluser]
not even the title and header?
#3

[eluser]Lorin[/eluser]
No, only a white page.

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

is

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

i can see a title, header and table. Of course without data from database.
#4

[eluser]jvicab[/eluser]
don't you have any kind of error message? seems to me it is a database connectivity issue, from what you wrote before, definitions are not complete:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = '******';
$db['default']['database'] = 'codeigniter';
$db['default']['dbdriver'] = ';
...

chek it out again, because if you not autoload the database library and everything works ok, that is the source of the problem
#5

[eluser]Lorin[/eluser]
In database.php I have:
Code:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = '******';
$db['default']['database'] = 'codeigniter';
$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_czech_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Username and password is ok. I'm using them to conect into mysql like:
Code:
mysql -u username -p

But the worst is, I don't see any error or warning message. Just a white page. So I don't know, what exactly is bad.
#6

[eluser]jvicab[/eluser]
try to split your code by pieces, first let database in autoload,php, opk? just comment the line in your controller: $data['query'] = $this->db->get('users'); // Query for data from database.
if it works (you see title and header) create an array to simulate couple of rows to check if your code works, like
$arr = array();
$arr[] = array('nick' => 'first_nick', 'passwd' => 'first_passwd, 'email' => 'first@demo.com');
$arr[] = array('nick' => 'second_nick', 'passwd' => 'second_passwd, 'email' => 'second@demo.com');
$data['query'] = $arr;

and see if you have your tworows table.



suggestion: anytime you work with areray like $data in this case, it is always a good practice to initialize as empty before using it, like $data = array(); as the first line in your index function
#7

[eluser]Lorin[/eluser]
Everything works only without database library. When database is loaded, white page come back. Any other idea?

users.php:
Code:
&lt;?php
    class Users extends CI_Controller {
        function index() {
            
            $arr = array();
            
            $arr[] = array(
                'nick' => 'test',
                'passwd' => 'oass',
                'email' => '[email protected]',
                'active' => '0'
            );
            
            $arr[] = array(
                'nick' => 'pepa',
                'passwd' => 'feqf',
                'email' => '[email protected]',
                'active' => '1'
            );

            $data = array(
                'title' => 'Some webpage title',
                'header' => 'User list',
                'query' => $arr,
            );

            //$data['query'] = $this->db->get('users');
            $this->load->view('users_view', $data);
        }
    }
?&gt;

And users_view.php:
Code:
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;? echo $title; ?&gt;&lt;/title&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <h1>&lt;? echo $header; ?&gt;&lt;/title&gt;
        <table>
            <tr><td>Jméno</td><td>Heslo</td><td>E-mail</td><td>Aktivován</td><td>Akce</td></tr>
            &lt;?php  foreach($query as $row) { ?&gt;
                <tr><td>&lt;? echo $row['nick']; ?&gt;</td> <td>&lt;? echo $row['passwd']; ?&gt;</td> <td>&lt;? echo $row['email']; ?&gt;</td> <td>&lt;?php if ($row['active'] == 0 ) { echo 'NE'; } else { echo 'ANO'; }?&gt;</td> <td>AKCE</td></tr>
            &lt;?php  } ?&gt;
        </table>
    &lt;/body&gt;
&lt;/html&gt;
#8

[eluser]Lorin[/eluser]
Have somebody any idea?
#9

[eluser]jvicab[/eluser]
only uncomment $autoload['libraries'] = array('database'); makes your page blank?
#10

[eluser]Lorin[/eluser]
Yes. If $autoload is set to 'database', I get blank page. If not, script works fine.




Theme © iAndrew 2016 - Forum software by © MyBB