json - Zend error Connection -
i have problem code, first code
<?php require_once('zend/json.php'); require_once('zend/db.php'); //require_once 'zend/db/adapter/pdo/pgsql.php'; class jsonvi { protected $_db; public function _koneksi () { try { $this->_db = zend_db::factory('pdo_pgsql',array( 'host'=>'localhost', 'username'=>'stet', 'password'=>'test', 'dbname'=>'test' )); return $this->_db; } catch(zend_db_exception $e) { return $e->getmessage(); } } public function getdata() { $db = $this->_koneksi(); try { $sql = "select * info "; $da = $db->fetchall($sql); $num = count($da); ($a=0;$a<$num;$a++) { $data = $db->fetchall($sql); return $data; } } catch (zend_db_exception $e) { return $e->getmessage(); } } } $view = new jsonvi(); $view = $view->getdata(); echo zend_json::encode($view); ?>
and works well, want make
<?php include ('table.php'); include ('config.php'); require_once('zend/json.php'); require_once('zend/db.php'); require_once 'zend/db/adapter/pdo/pgsql.php'; class jsonvi { protected $_db; public function _koneksi () { try { $this->_db = zend_db::factory('pdo_pgsql',array( 'host'=>$dbhost, 'username'=>$dbuser, 'password'=>$dbpass, 'dbname'=>$dbdb )); return $this->_db; } catch(zend_db_exception $e) { return $e->getmessage(); } } public function getdata() { $db = $this->_koneksi(); try { $sql = "select * ".$table; $da = $db->fetchall($sql); $num = count($da); ($a=0;$a<$num;$a++) { $data = $db->fetchall($sql); return $data; } } catch (zend_db_exception $e) { return $e->getmessage(); } } } $view = new jsonvi(); $view = $view->getdata(); echo zend_json::encode($view); ?>
i don't know whats wrong code, need help. error message notice: undefined variable: dbdb in c:\wamp....
config.php have code $dbpass = 'test';
, that, i'm creating simple web , want make code other database , aplikasi, changed config.php
, table.php
table.php maybe work if config.php
work.
thanks.
you using class , not paying attention variable scope.
i understand have variables values db connection in config.php
file. although these variables available inside script file include not available , accessible inside class. difference constants define (or variables inside global not idea).
you create config class inside config.php , set values properties instantiate class inside _koneksi
method. , use require_once instead of include.
update here's example similar file include ('config.php');
same declaring variables directly before class begins.
// variable defined outside class $foo = 'foo'; class demo { public $bar = 'bar'; function test() { $foobar = 'foobar'; echo 'class property $bar is:' . $this->bar . php_eol; echo 'local variable $foobar is:' . $foobar . php_eol; } function run() { if (isset($foo)) { echo 'global variable $foo is:' . $foo . php_eol; } else { // have problem. // variables outside class not available. echo 'global variable $foo not found' . php_eol; } } } $demo = new demo(); $demo->test(); // class property $bar is:bar // local variable $foobar is:foobar $demo->run(); // global variable $foo not found echo 'variable $foo is: ' . $foo . php_eol; // class property $bar is:bar
Comments
Post a Comment