Search code examples
mysqldatabasecodeigniterremote-connection

Codeigniter remote connection does not return queries


I have a local and a remote connection with my mysql database. The local connection works just fine. But the remote connection, while it makes a connection, it does not return anything. I usually get the following:

Fatal error: Call to a member function result() on a non-object

I use for the remote connection the following configuration:



    $db['mydb']['hostname'] = "ip_address_of_database";
    $db['mydb']['username'] = "username";
    $db['mydb']['password'] = "password";
    $db['mydb']['database'] = "database";
    $db['mydb']['dbdriver'] = "mysql";
    $db['mydb']['dbprefix'] = "";
    $db['mydb']['pconnect'] = FALSE;
    $db['mydb']['db_debug'] = FALSE;
    $db['mydb']['cache_on'] = FALSE;
    $db['mydb']['cachedir'] = "";
    $db['mydb']['char_set'] = "utf8";
    $db['mydb']['dbcollat'] = "utf8_general_ci";

In my function that accesses the database I check if there is a connection with the remote server and then I try to retrieve data.



    $mydb = $this->load->database('mydb', TRUE);
    if (!isset($mydb->conn_id) && !is_resource($mydb->conn_id)) {
        $error = 'database is not connected';
        return $error;
    }else{
        $query = $mydb->query("SELECT * FROM database LIMIT 1;"); 
        return $query->result();            
         }

This works fine in the localhost database but not in the remote database. I allways get the error Fatal error: Call to a member function result() on a non-object

Can you please help? What am I doing wrong? I stuck on this.


Solution

  • Finally, I found the solution after contacting my web hosting provider. The issue had to do with the Remote database access and their servers. The IP address exception and the domain name that I had added didn’t do the job. I had to add an internal domain name that my host was using in order the Remote database access to be allowed. I spent 2-3 hours chatting with them in order to find a solution. Anyway now is solved. I am posting that FYI.