我想访问数据库“mysql”,我读到我们可以通过在application.ini文件中写下这些行来定义db adapter和db配置
resources.db.adapter = MYSQLI
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password =123456
resources.db.params.dbname = visits_db
我想获取 $ db 对象以便用于执行sql语句
$db->insert("inspection_visits_tb",
$insepctionVisitData = array(
'day' => $visit->getDay(),
'date' => $visit->getDate(),
'target' => $visit->getTarget()
));
我想从application.ini文件中获取它而不是这个
require_once 'Zend/Db.php';
$db = Zend_Db::factory("MYSQLI",
array(
"host" => "localhost",
"dbname" => "visits_db",
"username" => "root",
"password" => "123456")
);
因为我只想在一个地方定义数据库适配器。我该怎么做才能获得 $ db 对象?
答案 0 :(得分:2)
在db
中设置application.ini
资源,就像您一样。添加isDefaultAdapter
选项。
resources.db.adapter = "MYSQLI"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "123456"
resources.db.params.dbname = "visits_db"
resources.db.isDefaultAdapter = true
然后获取$db
对象......
protected function _initDb()
{
$this->bootstrap('db');
$db = $this->getResource('db');
Zend_Registry::set('db', $db);
}
$db = Zend_Db_Table::getDefaultAdapter();
$table = new Zend_Db_Table('bugs');
$select = $table->select()...
有关更多示例,另请参阅ZF Quickstart,Zend_Db_Table,Zend_Db_Select。