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?