Welcome Guest, Not a member yet? Register   Sign In
Empty database output within CI
#1

[eluser]den-javamaniac[/eluser]
Hi. I'm building a simple app (using video tutorials and reference documentation) and trying to test DB result output. But unfortunately all I'm getting is an array of size 0. Here's the controller code excerpt:

Code:
$data['query'] = $this->db->query('SELECT role_id, role_privilege FROM role');
$this->load->view('welcome_message', $data);
And a view code excerpt:
Code:
<?php
        echo count($query->result_array())."<br/>";
        foreach ($query->result() as $row){
            echo $row->role_id . '<br/>';
            echo $row->role_privilege . '<br/>';
        }
        echo 'Total result '.$query->num_rows();
    ?&gt;
And what I get is next:

Code:
0
Total result
Running query from a command line gives a 2 rowed output as it's supposed to be. Can someone point out what I'm missing or where might be a problem?
#2

[eluser]WanWizard[/eluser]
What is $query in this example? Do you mean $data['query'], which holds the result of the query?
#3

[eluser]den-javamaniac[/eluser]
Yes, that's correct. That's how it's done in a video tutorial.

NOTE:This issue is also discussed here.
#4

[eluser]Aken[/eluser]
var_dump your query variable inside the view, to see what exactly is getting passed to it.

If it's NULL or something similar, then there's an error with your actual query command somewhere.

Did you load the DB library? Did you add your database credentials to the DB config file? etc etc.
#5

[eluser]WanWizard[/eluser]
[quote author="den-javamaniac" date="1273367365"]Yes, that's correct. That's how it's done in a video tutorial.[/quote]
I mean that in your example you fetch the query result in the variable $data['query'], then you continue with using the variable $query, which is never assigned a value.

I assume this is an error in the tutorial?!
#6

[eluser]den-javamaniac[/eluser]
I presume that your assumption is incorrect. If I use $data['query'], I get a "Undefined variable: data" error. Besides, if using
Code:
$data['title'] = "Welcome to CodeIgniter";
$data['msg'] = array('this is me', 'and me');
I can still get my
Code:
&lt;?php foreach ($msg as $string): ?&gt;
        <p>
            &lt;?php echo $string ?&gt;
        </p>
&lt;?php endforeach; ?&gt;
working correctly. Any other ideas?
#7

[eluser]den-javamaniac[/eluser]
Well, this here problem is basically not a CI related one, but strange thing is that CI couldn't track this error. Here's what was going on:

While installing php I haven't specified --with-mysql-socket parameter and it looks like php tried to use a /tmp/mysql.sock (default one) which obviously was not specified in my.cnf. So CI tried to bind to a nonexistent socket. Changing mysql params in a php.ini solved the problem.




Theme © iAndrew 2016 - Forum software by © MyBB