Zend_Db 的包装失败

发布于 2024-10-04 02:20:44 字数 1565 浏览 9 评论 0原文

总之,

我编写了以下包装器,它扩展了 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 方法失败吗?

谢谢

All,

I wrote the following wrapper that extends Zend_Db class to provide db connectivity to my entire application. The db connectivity works fine, but it fails to execute queries when invoked from a different class.

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;
 }
}

Trying to execute query in a PHP file that includes Testapp_DB - fails..

<?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);
?>

Can someone please explain, why the fetchAll method fails?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赤濁 2024-10-11 02:20:44

*Zend_Db* 除了factory() 之外没有任何方法。

我真的不明白,你想用这种方式解决什么问题,但据我所知,你需要这样调用 query()

$db->db->query();

*Zend_Db* doesnt have any method other than factory().

I dont really get, what you are trying to solve this way, but as far as I can see you need to call query() this way

$db->db->query();
云醉月微眠 2024-10-11 02:20:44

也许不使用包装器,而是尝试以下操作:

// 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')。而且,每个请求只需建立一个数据库连接一次。

Perhaps instead of using your wrapper, try this:

// 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 = ...

Then, your Zend_Db_Table objects will have connectivity, and in addition you can grab the connection using Zend_Registry::get('database'). And, you'll make only one database connection once per request.

埋情葬爱 2024-10-11 02:20:44

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

$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

Try to create a SQL file like schema.mysql.sql,
then test it on phpMyAdmin and when it's free of bugs run this code:

$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);

Hope this helps! http://www.unexpectedit.com/zend-php/zend-db-exec-a-sql-file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文