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' => 'mydb23.surftown.se',
'database' => 'andclic_app',
'username' => 'myusername',
'password' => 'mypassword',
'persistent' => FALSE,
And then I try th开发者_开发问答is 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;
66
67 throw new Database_Exception(':error',
68 array(':error' => $e->getMessage()),
69 $e->getCode());
70 }
71
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:
Database::instance('default');
You don't have to specify default
, it's default
by default.
$default->query(Database::SELECT, 'SELECT * FROM products', TRUE);
Make sure you call the ->execute()
method to run the query.
精彩评论