問題描述
我想在沒有 Zend_Framework 的情況下使用 Zend_Db.我想為我現有的不是使用 Zend Framework 制作的網站合并 Zend_Db.是否可以像這樣使用 Zend_Db?你能推薦好的教程或例子嗎?
I want using Zend_Db without Zend_Framework. I want incorporate Zend_Db for my existing website that was not made using Zend Framework. Is it possible to use Zend_Db like this? Can you recommend good tutorial or example how to do it good?
推薦答案
在某種程度上,這取決于您使用的 Web 框架.但是,總的來說,Zend_Db 文檔在這方面.
To some extent, this depends upon the web framework you are using. But, in general, the Zend_Db documentation is pretty clear in this regard.
在您的引導程序中創建一個適配器實例.舉個例子:
Create an adapter instance in your bootstrap. As an example:
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
如果您打算使用 Zend_Db_Table
,那么您可以將其設為默認適配器:
If you plan to use Zend_Db_Table
, then you can make this the default adapter:
Zend_Db_Table::setDefaultAdapter($db);
無論如何,將此適配器保存在您可以訪問的地方會很有幫助.例如:
In any case, it's helpful to have this adapter saved someplace where you can access it. For example:
Zend_Registry::set('db', $db);
然后在您的下游代碼中,使用此適配器為 select()
、insert()
、update()
、delete()
等:
Then in your downstream code, use this adapter to create queries for select()
, insert()
, update()
, delete()
, etc:
$db = Zend_Registry::get('db');
$select = $db->select()
->from('posts')
->where('cat_id = ?', $catId)
->order('date_posted DESC')
->limit(5);
$rows = $db->fetchAll($select);
希望這會有所幫助.干杯!
Hope this helps. Cheers!
這篇關于Zend_Db 沒有 Zend 框架的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!