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 方法失败吗?
谢谢
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
*Zend_Db* 除了factory() 之外没有任何方法。
我真的不明白,你想用这种方式解决什么问题,但据我所知,你需要这样调用 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
也许不使用包装器,而是尝试以下操作:
然后,您的
Zend_Db_Table
对象将具有连接性,此外,您可以使用Zend_Registry::get('database')
。而且,每个请求只需建立一个数据库连接一次。Perhaps instead of using your wrapper, try this:
Then, your
Zend_Db_Table
objects will have connectivity, and in addition you can grab the connection usingZend_Registry::get('database')
. And, you'll make only one database connection once per request.尝试创建一个类似 schema.mysql.sql 的 SQL 文件,
然后在 phpMyAdmin 上测试它,当它没有错误时运行此代码:
希望这有帮助! 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:
Hope this helps! http://www.unexpectedit.com/zend-php/zend-db-exec-a-sql-file