php - Cannot connect to remote MySql Server in Kohana


I'm trying out Kohana for a php application, and I have a simple database test working when connecting to my localhost installation of MySql. But when I try to change the connection to a remote MySql database on a web host (Surftown), it doesn't work.

Here are the connection data from database.php (the 'default' connection):

        'hostname'   => '',
        'database'   => 'andclic_app',
        'username'   => 'myusername',
        'password'   => 'mypassword',
        'persistent' => FALSE,

And then I try this in my controller:

$default = Database::instance('default');
$result = $default->query(Database::SELECT, 'SELECT * FROM products', TRUE);

But I get an error saying no connection exists:

62      catch (Exception $e)
63      {
64          // No connection exists
65          $this->_connection = NULL;
67          throw new Database_Exception(':error',
68              array(':error' => $e->getMessage()),
69              $e->getCode());
70      }
72      // \xFF is a better delimiter, but the PHP driver uses underscore

So what am I doing wrong? I also tried using the ip number instead of the host name, and I tried appending the port (:3306), but neither helped.




This issue occurs when you have php activated to show deprecated errors. The mysql driver is based on the mysql_connect library which has been deprecated. When your php throws deprecated error on this library the Kohana class cannot handle the error. To avoid this error you need to add the following line of code in : modules->database->classes->kohana->database->mysql.php

Look for the following line of code:

    // Prevent this information from showing up in traces
    unset($this->_config['connection']['username'], $this->_config['connection']['password']);

Add the following line to the file right after the previous line:

    //Added to suppress deprecated error due to deprecated driver
    error_reporting(E_ALL ^ E_DEPRECATED);

After adding this line, the deprecated error code will be suppressed and you will be able to use the mysql_connect extension.

This is a temporary workaround since eventually you need to migrate your code to the new supported extension mysqlite

This issue occurs primariy with the latests versions of php. You won't experience this behavior in an older version of php.

Hope this helps it took me a while to figure this one out.




Make sure you're able to connect to the remote MySQL server without Kohana. Few other tips:


You don't have to specifydefault, it'sdefault by default.

$default->query(Database::SELECT, 'SELECT * FROM products', TRUE);

Make sure you call the->execute() method to run the query.

People are also looking for solutions to the problem: searching the html source code of a url using curl in php


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.