0

I try to retrieve a single value from MySQL database, and I'm using Zend Framework. Here is a part of my application.ini which I use to store database information.

[production]
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = 
resources.db.params.dbname = accounts_db
resources.db.isDefaultTableAdapter = true

Here is the Bootstrap.php:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initDbRegister()
    {
        $db = $this->bootstrap('Db')->getResource('Db');
        $config = new Zend_Config((array('database'=>array('params'=>$db))));
        Zend_Registry::set('config',$config);
    }
}

Here is my code which I use to retrieve data from the database.

class User
{
    public function authenticate()
    {
         $success = false;
         $this->encrypt();//use to hash the provided password
         try{
             $conf = Zend_Registry::get('config');
             $db = Zend_Db::factory($conf->database);
             $select = $db->select()
                          ->from('user','COUNT(*)')
                          ->where('userName = ?',$this->userName)
                          ->where('password = ?',$this->password);
            $stmt = $select->query();
            $result = $stmt->fetchColumn(0);
            if($result == 1){
                $success = true;
                return $success;
            }

        } catch (Exception $e) {
            $e->getMessage();
            return $success;
        }
    }
}

NOTE: I have tested this earlier using mysql_connect() and mysql_query(). Then it worked fine. If the query succeed it displays the main page or otherwise it redirects to the welcome page. Then I only changed the User class and now it just displays a blank page when trying to login. Why is this?

ByteNudger
  • 1,545
  • 5
  • 29
  • 37
PHP-Zend
  • 301
  • 1
  • 4
  • 20
  • add this line to your .htacces file to set development mode and show errors in the broweser: `SetEnv APPLICATION_ENV development`. – RockyFord Jul 15 '12 at 06:54
  • also you can remove your current $conf and $db variables and use `$db = Zend_Db_Table::getDefaultAdapter();` which is why you enabled `isDefaultTableAdapter = true` – RockyFord Jul 15 '12 at 06:57
  • I only changed the `.htaccess` file. Then it gave this error : `Fatal error: Call to undefined method Zend_Db_Adapter_Pdo_Mysql::toArray() in C:\wamp\www\...\library\Zend\Db.php on line 211` This error occurs as the application can't find the relavent method in the Zend library, isn't it? – PHP-Zend Jul 15 '12 at 07:37
  • This error occurred because you passed in an array instead of a string or object as the first argument in you Zend_Db::factory. Try changing it to `$db = Zend_Db::factory('Pdo_Mysql', $conf->database);` I think this should work if your array is properly formatted. – RockyFord Jul 15 '12 at 11:05

0 Answers0