簡體   English   中英

Zend_Db的包裝失敗

[英]Wrapper to Zend_Db is failing

所有,

我編寫了以下包裝程序,該包裝程序擴展了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方法失敗嗎?

謝謝

* Zend_Db *除了factory()之外沒有任何其他方法。

我真的不明白,您正在嘗試以這種方式解決什么,但是據我所知,您需要以這種方式調用query()

$db->db->query();

也許不使用包裝器,請嘗試以下操作:

// 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')連接。 而且,每個請求僅建立一次數據庫連接。

嘗試創建一個類似於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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM