Welcome Guest, Not a member yet? Register   Sign In
Database results will not display. New to CI
#1

[eluser]cpeele[/eluser]
Hey guys, I am very new to codeIgniter and am trying to get my page to display some database results but no rows show.

1) I have data in my database

2) I have set autoloading

3) Here is my database configuration:

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

4) Here is my Controller

Code:
<?php

class Questions extends Controller
{    
    function index()
    {
        $data['query'] = $this->db->get('questions');
        
        $this->load->view('questions', $data);
    }
}

?>

5) Here is my view

Code:
<?php if($query->num_row() > 0): ?>
this does not show :(
<?php endif; ?>

<ul>
&lt;?php foreach($query as $row): ?&gt;
    <li>&lt;?=$row->name?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>

6) Here is my output when I do a print_r($data);

Code:
Array ( [query] => CI_DB_mysql_result Object ( [conn_id] => Resource id #28 [result_id] => Resource id #29 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 2 [row_data] => ) )


Please if anyone can help me I would really appreciate it. I would like to start using CI over CakePHP but I can't even get this to dispaly database results.

Thanks for any help,

Chris
#2

[eluser]Stelian Mocanita[/eluser]
Code:
$query = $this->db->get('questions');
$data['query'] = $query->result_array();

That will convert yout mysql resource into the array you need.
#3

[eluser]Dam1an[/eluser]
[quote author="Stelian Mocanita" date="1240694940"]
Code:
$query = $this->db->get('questions');
$data['query'] = $query->result_array();

That will convert yout mysql resource into the array you need.[/quote]

But he's using the query object in the view (num_rows)
And he's using it as an object, not an array

Changing the foreach loop to be
Code:
&lt;?php foreach($query->result() as $row): ?&gt;
should fix it
#4

[eluser]Stelian Mocanita[/eluser]
My bad, didn't see the view part

just use
Code:
$data['query'] = $query->result()

That will pull an object data from the resource so you don't have to change the view file.
#5

[eluser]Dam1an[/eluser]
[quote author="Stelian Mocanita" date="1240702662"]My bad, didn't see the view part

just use
Code:
$data['query'] = $query->result()

That will pull an object data from the resource so you don't have to change the view file.[/quote]

He will still have to change the view, as he calls num_rows, which is called on the query object
So he either converts the query to a result after that, or uses sizeof on the results
#6

[eluser]cpeele[/eluser]
Hey guys, thanks for all the responses.

I tried all your suggestions to no avail unfortunately

Code:
&lt;?php

class Questions extends Controller
{    
    function index()
    {

        $query = $this->db->get('questions');
        $data['query'] = $query->result();
    }
}

?&gt;

I even tried using
Code:
$data['query'] = $query->result_array();

here's the view

Code:
<ul>
&lt;?php foreach($query->result() as $row): ?&gt;
    <li>&lt;?=$row->name?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>

it shouldn't be this hard to pull some data...any additional ideas would be greatly appreciated.
#7

[eluser]Dam1an[/eluser]
you now alppear to be calling ->result() on the query in the controller, and you try calling it on what is now the result (although called query in the view

try
Code:
&lt;?php

class Questions extends Controller
{    
    function index()
    {

        $query = $this->db->get('questions');
        $data['questions'] = $query->result();
    }
}

?&gt;

// view
<ul>
&lt;?php foreach($questions as $question): ?&gt;
    <li>&lt;?=$question->name?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>
#8

[eluser]cpeele[/eluser]
unfortunately that doesn't appear to work either. but thanks for the help
#9

[eluser]Dam1an[/eluser]
Does that give you an error, or just doesn't have the results you expect?
#10

[eluser]cpeele[/eluser]
Hello Smile

It does't give me an error or any results Sad




Theme © iAndrew 2016 - Forum software by © MyBB