Zend 错误连接

Zend error Connection

我的代码有问题,第一个代码就像

<?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 * from info   ";
                $da = $db->fetchall($sql);
                $num = count($da);      
                for ($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);

?>

而且效果很好,但我想把它做得像

<?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 * from ".$table;
                $da = $db->fetchall($sql);
                $num = count($da);      
                for ($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);      
?>

我不知道这段代码有什么问题,我需要帮助。 错误信息 Notice: Undefined variable: dbdb in C:\wamp.... config.php 只有像 $dbpass = 'test'; 这样的代码,我正在创建一个简单的网站,我想将我的代码用于其他数据库和 aplikasi,所以我只更改了 config.phptable.php 因为 table.php 如果 config.php 有效,则可能会有效。

谢谢。

您正在使用 class 而没有注意变量范围。

我了解到您在 config.php 文件中有包含数据库连接值的变量。尽管这些变量在包含 include 的脚本文件中可用,但它们在您的 class 中不可用且不可访问。唯一的区别是任何带有 define 的常量(或全局变量,这不是一个好主意)。

您可以在 config.php 中创建配置 class 并将值设置为属性,然后在 _koneksi 方法中实例化 class。然后你会使用 require_once 而不是 include.

更新 所以这是一个类似于您的文件的示例,其中您的 include ('config.php'); 与在 class 开始之前直接声明您的变量基本相同。

// variable defined outside the 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 {
            // This is where you have your problem.
            // Variables from outside the class are 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