Zend_Db的包装器失败了

时间:2010-11-24 19:41:01

标签: php mysql zend-framework zend-db

所有

我编写了以下包装器,它扩展了Zend_Db类,为我的整个应用程序提供了数据库连接。数据库连接工作正常,但从其他类调用时无法执行查询。

class Testapp_DB extends Zend_Db {

        //Declare member variables.
        public $db          = null;
        public $db_host     = "";
        public $db_database = "";
        public $db_username = "";
        public $db_password = "";


 public function __construct()
        {

            //Read Database Info from Config File
            $this->db_host      = Testapp_Registry::get('config')->db->mysql->host;
            $this->db_database  = Testapp_Registry::get('config')->db->mysql->dbname;
            $this->db_username  = Testapp_Registry::get('config')->db->mysql->username;
            $this->db_password  = Testapp_Registry::get('config')->db->mysql->password;

            $db = Zend_Db::factory('Mysqli', array(
                'host'     => $this->db_host,
                'username' => $this->db_username,
                'password' => $this->db_password,
                'dbname'   => $this->db_database
            ));

            $db->getConnection(); //Works fine
            $this->db = $db;
 }
}

尝试在包含Testapp_DB的PHP文件中执行查询 - 失败..

<?php

 $db = new Testapp_DB();
 print_r($db);  //I can see the DB object here
 $sql = 'SELECT * FROM tb_Log';
 $result = $db->fetchAll($sql, 2); //This method fails.. Don't know why. Any ideas?
 print_r($result);
?>

有人可以解释,为什么fetchAll方法会失败?

由于

3 个答案:

答案 0 :(得分:1)

* Zend_Db *没有 factory()以外的任何方法。

我真的没有,你想要用这种方式解决,但据我所知,你需要以这种方式调用 query()

$db->db->query();

答案 1 :(得分:0)

也许不是使用你的包装器,试试这个:

// Note: change your config to have a 'params' entry
$db = Zend_Db::factory(Testapp_Registry::get('config')->db->mysql);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
Zend_Registry::set('database', $db);

// in your config
...mysql.params.host = ...
...mysql.params.dbname = ...
...mysql.params.username = ...
...mysql.params.password = ...

然后,您的Zend_Db_Table个对象将具有连接性,此外,您可以使用Zend_Registry::get('database')获取连接。而且,每个请求只需要一次数据库连接。

答案 2 :(得分:0)

尝试创建一个类似schema.mysql.sql的SQL文件, 然后在phpMyAdmin上测试它,当它没有bug时运行这段代码:

$bootstrap = $application->getBootstrap();
$bootstrap->bootstrap(‘db’);
$config = $bootstrap->getResource(‘db’)->getConfig();
$runCommand = “mysql -h “.$config["host"].” -P “.$config["port"].” -u’”.$config["username"].”‘ -p’”.$config["password"].”‘ “.$config["dbname"].” < “.dirname(__FILE__) . ‘/schema.mysql.sql’;
echo $runCommand;
system($runCommand);

希望这有帮助! http://www.unexpectedit.com/zend-php/zend-db-exec-a-sql-file