Welcome Guest, Not a member yet? Register   Sign In
Query returning no results?
#1

[eluser]Xabi87[/eluser]
Hi,

I've just setup a new codeigniter instance, everything except the database connection is working fine.

I've setup my config as follows (in config/database.php):


Code:
$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = "174.123.123.123";
$db['default']['port'] = "3306";
$db['default']['username'] = "test_user";
$db['default']['password'] = "mypassword";
$db['default']['database'] = "shora_template";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

I've setup a query in a test model, and I'm seeing no errors (should I?), only no results:
Code:
$query = $this->db->get('Daemon');
echo "num rows:" . $query->num_rows();

This returns nothing.

The query definitely returns a result, I'm using a mysql query browser session with the exact same connection info, same port, same user, and the same query is returning a result.

I've enabled the profiler on the page and I can see the correct query in the 'DATABASE' section.

Any ideas? I'd appreciate some further troubleshooting steps please!

Thanks,

Xabi
#2

[eluser]techgnome[/eluser]
What does your controller and view look like? What about your model? You've given some snips there, but with out any context on how it all goes together.

-tg
#3

[eluser]Xabi87[/eluser]
Sorry, here's more info:

The Controller:
Code:
<?php

class Daemon extends Controller {

    function Daemon()
    {
        
        parent::Controller();
        
        //load daemon model
        $this->load->model("daemon_model");
        //enable profiler
        $this->output->enable_profiler(true);
    }
    
    function index()
    {
        
        $data['daemons'] = $this->daemon_model->get_daemons();
        $this->load->view('daemon/daemon_view', $data);

    }
}

/* End of file daemon.php */
/* Location: ./system/application/controllers/daemon.php */


The Model:
Code:
<?php
class Daemon_model extends Model {

    function Daemon_model()
    {    
        parent::Model();
    }
    function get_daemons(){
        
        $query = $this->db->get('Daemon');
        echo "num rows:" . $query->num_rows();
        
        //return 2D array
        return to_2D_array($query);
    }

}
?>

The view is not important. I'm simply trying to echo the query result from the model, and it's not working. Should it throw an error if the db connection is not correct?

Thanks
#4

[eluser]veritascs[/eluser]
Try something a little more simple (just to see if everything works). Using some examples from http://ellislab.com/codeigniter/user-gui...mples.html

database.php
Code:
$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "<hostname>";
$db['default']['username'] = "<username>";
$db['default']['password'] = "<password>";
$db['default']['database'] = "<databasename>";
$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";

controller testDB.php
Code:
&lt;?php

class testDB extends Controller {
    function index()
    {
        $this->load->database();
        $query = $this->db->query('SELECT product_id, product_name from products');

        foreach ($query->result() as $row)
        {
            echo $row->product_id;
            echo $row->product_name;
            echo "<br />";
        }

        echo 'Total Results: ' . $query->num_rows();
    }
}

/* End of file testDB.php */
/* Location: ./system/application/controllers/testDB.php */

Notice I have debug on and am not calling a model or view. Does something more simple like this work for you? //fyi http://wallaholics.ever-flow.net/testDB
#5

[eluser]CroNiX[/eluser]
And you are autoloading your database library I assume?

Edit: looks like you solved it by the results on that page?
#6

[eluser]InsiteFX[/eluser]
Code:
echo "num rows:" . $query->num_rows();

// Should be this
echo "num rows:" . $this->query->num_rows();

// return 2D array
return to_2D_array($this->query);

InsiteFX
#7

[eluser]Developer13[/eluser]
@InsiteFX - what part of what the OP posted suggests that $query should be $this->query? I don't think your response is valid.
#8

[eluser]Sweetsugar70[/eluser]
Hi guys new here,and find topics here very interesting,
Hope I can join in your discussions here,
God Bless!


how to deal with depression
#9

[eluser]InsiteFX[/eluser]
[quote author="Developer13" date="1289989256"]@InsiteFX - what part of what the OP posted suggests that $query should be $this->query? I don't think your response is valid.[/quote]

Developer13,

Sorry, but if you were here every morning and night going through all the topics
looking for and flagging all the SPAM! I am sure you would make a mistake too!

Then I have to meassge one of the EllisLab staff to get them deleted!

Unless you want to spend all the time here and do it for me?

InsiteFX
#10

[eluser]Dennis Rasmussen[/eluser]
InsiteFX, don't be a douche... really.
And I'm sure D13 also hits the report button once in a while - like the rest of us.

For anyone reading this topic later and having trouble themselves, I believe the OP found out to always load the database (based on the info we got).




Theme © iAndrew 2016 - Forum software by © MyBB