In this post i will tell you how to connect database in zend framework using Zend_Db.

After initiating the Zend framework project make a folder like this

Zend Project -> applicaiton -> models -> Db -> Db.php

we make a class name  “Db_Db” and define a static method “conn()”

 

Code

class Db_Db
{	
	public static function conn()
	{
		try 
    	{
    		$pdoParams = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8;');
    		
    		$connParams = array("host" => "localhost",
    		"port" => "PORT",
    		"username" => "DATABASE_USERNAME",
    		"password" => "DATABASE_PASSWORD",
    		"dbname" => "DATABASE_NAME",
    		"driver_options" => $pdoParams
    		);
    		
    		$db = new Zend_Db_Adapter_Pdo_Mysql($connParams);
    		return $db;
    	}
    	catch (Zend_Db_Exception $err)
    	{
    		echo $err->getMessage();
    	}    	
	}
}
Advertisements