views:

47

answers:

1

Hi, I am using Zend Framework and my application works on localhost but produces the can't connect to MySQL server error when I'm trying to connect to the database I've uploaded on a live server. I've tried handling exceptions and Zend_Exception catches it("perhaps factory() failed to load the specified Adapter class"). I've emailed the webmaster and he told me that Phpmyadmin is working fine so there should be no problem with connecting php to mysql. What else can I do? The lines that produce the error are the following:

    $db = Zend_Registry::get('db');
    $sql = 'SELECT * FROM department';
    $result = $db->fetchAll($sql);  

Is there something specific I can ask the webmaster to look at?

EDIT:

in my bootstrap:

public function _initDB()
{   

    $dbOptions = $this->getOption('db');
    $db = Zend_Db::factory($dbOptions['adapter'], $dbOptions['params']);
    Zend_Registry::set('db', $db);
    Zend_Db_Table_Abstract::setDefaultAdapter($db);
}

in my application.ini:

db.adapter = PDO_MYSQL
db.params.host = agila.upm.edu.ph
db.params.username = FacultyDB 
db.params.password = *********
db.params.dbname = FacultyDB
+1  A: 

Since you are convinced the settings are ok in application.ini are you initialising your db in bootstrap?

protected function _initDb() {

    $registry = Zend_Registry::getInstance();

    $db = Zend_Db::factory($registry->config->db->adapter,
                    $registry->config->db->database->toArray());

    Zend_Db_Table::setDefaultAdapter($db);
    Zend_Registry::set('db', $db);
}

try that in application.ini:

[db]
adapter = PDO_MYSQL
database.host = agila.upm.edu.ph
database.username = FacultyDB 
database.password = *********
database.dbname = FacultyDB 

Try configuring with that and my bootstrap code and see if it now works. I remember something about zend_db being picky about configuration from application.ini. I could be wrong its vague.

Iznogood
This is what I have in my application.ini: db.adapter = PDO_MYSQLdb.params.host = agila.upm.edu.phdb.params.username = FacultyDB db.params.password = ********db.params.dbname = FacultyDB and this is what my _initDB looks like: $dbOptions = $this->getOption('db'); $db = Zend_Db::factory($dbOptions['adapter'], $dbOptions['params']); Zend_Registry::set('db', $db); Zend_Db_Table_Abstract::setDefaultAdapter($db);
@user429148 You know a lot of servers refuse external connections to MySQL. You could maybe try asking the admin about that.
Iznogood
Thanks for the suggestions. I've asked some classmates and I'm the only one who has this problem(but I'm the only one who's using ZF). I've also tried running the application at school so it can't be my internet connection.
@user429148 Well then somethign must be wrong on your end. You could always try using classical MySQL(mysql_connect()) to check that you can really connect. But PDO should not have any problems (normally).
Iznogood
@user429148 I notice that the user name is the same as the db name. Is that right?
Iznogood
Yes, they are the same.
@user429148 Could you try using my syntax for application.ini and bootstrap to be sure its not something with that? I edited my answer with code for you to try.
Iznogood
EDIT: I tried using your code but it produces this error: Fatal error: Call to a member function toArray() on a non-object... I also just tried mysql_connect and it can't connect either. So ZF can't be the problem now..
@user429148 Well then somethign is up with your credentials on the server look it up with the admin. At least its not just PDO since valilla MySQL wont connect either.
Iznogood
Yeah, I'm emailing him right now. Thanks for helping me narrow down the problem.
@user429148 no problems. You could always give me a vote or accept my answer if it helped in the end. :)
Iznogood