Koneksi database MySQL dengan Zendframework 1.12

Untuk melakukan koneksi ke database mysql dari zendframework 1.12. yang perlu dilakukan adalah

pertama menambahkan script di dalam file /public/index.php dari keadaan default seperti berikut

 

<?php

// Define path to application directory
defined(‘APPLICATION_PATH’)
|| define(‘APPLICATION_PATH’, realpath(dirname(__FILE__) . ‘/../application’));

// Define application environment
defined(‘APPLICATION_ENV’)
|| define(‘APPLICATION_ENV’, (getenv(‘APPLICATION_ENV’) ? getenv(‘APPLICATION_ENV’) : ‘production’));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . ‘/../library’),
get_include_path(),
)));

/** Zend_Application */
require_once ‘Zend/Application.php’;

// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . ‘/configs/application.ini’
);
$application->bootstrap()
->run();

menjadi seperti dibawah ini

<?php

// Define path to application directory
defined(‘APPLICATION_PATH’)
|| define(‘APPLICATION_PATH’, realpath(dirname(__FILE__) . ‘/../application’));

// Define application environment
defined(‘APPLICATION_ENV’)
|| define(‘APPLICATION_ENV’, (getenv(‘APPLICATION_ENV’) ? getenv(‘APPLICATION_ENV’) : ‘production’));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . ‘/../library’),
get_include_path(),
)));

/** Zend_Application */
require_once ‘Zend/Application.php’;

//
require_once ‘Zend/Config/Ini.php’;
require_once ‘Zend/Registry.php’;
require_once ‘Zend/Db.php’;
require_once ‘Zend/Db/Table/Abstract.php’;
require_once ‘Zend/Filter/Input.php’;

// get params db from application.ini
$config = new Zend_Config_Ini(‘../application/configs/application.ini’, ‘db’);

// set database configuration parameters
$adapter = $config->db->adapter;
$params = $config->db->params;

// initialize database
$db = Zend_Db::factory($adapter, $params);

// set database as default
Zend_Db_Table_Abstract::setDefaultAdapter($db);

// save database object in Zend_Registry
Zend_Registry::set(‘db’, $db);

// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . ‘/configs/application.ini’
);

$application->bootstrap()
->run();

lalu yang kedua adalah konfigurasi dengan menambahkan kode berikut di dalam file application/config/application.ini

[db]
db.adapter = “mysql”
db.params.host = “localhost”
db.params.username = “root”
db.params.password = “”
db.params.dbname = “dbname”

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s