Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Zend framework 如何在一个文件中连接zend_Db_Zend Framework_Zend Db - Fatal编程技术网

Zend framework 如何在一个文件中连接zend_Db

Zend framework 如何在一个文件中连接zend_Db,zend-framework,zend-db,Zend Framework,Zend Db,当我想将zend framework中的数据库与zend_Db连接时,在每个控制器或模型中必须编写以下代码: $params = array( 'host' => '127.0.0.1', 'username' => 'webuser', 'password' => 'xxxxxxxx', 'dbname' => 'test', 'charset' => 'utf8' ); 但当使用原则时,

当我想将zend framework中的数据库与zend_Db连接时,在每个控制器或模型中必须编写以下代码:

$params = array(
'host'           => '127.0.0.1',
'username'       => 'webuser',
'password'       => 'xxxxxxxx',
'dbname'         => 'test',
'charset'        => 'utf8'
);
但当使用原则时,在application.ini中编写此代码就足够了

doctrine.dsn = "mysql://user:pass@localhost/dbase"
如何使用zend_Db并在每个文件中设置外部连接?

打开

application/configs/application.ini
添加以下行

resources.db.adapter = PDO_MYSQL
resources.db.isDefaultAdapter = true
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = 
resources.db.params.dbname = foo
然后在Bootstrap.php中

然后,无论何时何地需要db实例,只要

$db = Zend_Db_Table::getDefaultAdapter();
打开

添加以下行

resources.db.adapter = PDO_MYSQL
resources.db.isDefaultAdapter = true
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = 
resources.db.params.dbname = foo
然后在Bootstrap.php中

然后,无论何时何地需要db实例,只要

$db = Zend_Db_Table::getDefaultAdapter();

有两种方法,一种是使用configuration.ini文件 根据该守则:

resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = ''
resources.db.params.dbname = dbname
resources.db.params.driver_options.1002 = "SET NAMES utf8;"
或者使用hte适配器类

$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'     => '127.0.0.1',
    'username' => 'webuser',
    'password' => 'xxxxxxxx',
    'dbname'   => 'test'
));

不要在bootstrap.php文件中放入任何函数。有两种方法,一种是使用configuration.ini文件 根据该守则:

resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = ''
resources.db.params.dbname = dbname
resources.db.params.driver_options.1002 = "SET NAMES utf8;"
或者使用hte适配器类

$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'     => '127.0.0.1',
    'username' => 'webuser',
    'password' => 'xxxxxxxx',
    'dbname'   => 'test'
));

不要在bootstrap.php文件中放入任何函数

我认为这里不需要bootstrap代码。AFAIK默认情况下,数据库资源是引导的。至少我从来没有在我的任何项目中这样做过。因为我提到了“where-ever或where”,其中包括引导,但没有这样做,你就不能使用Zend_Db_Table::getDefaultAdapter();内部引导。我认为这里不需要引导代码。AFAIK默认情况下,数据库资源是引导的。至少我从来没有在我的任何项目中这样做过。因为我提到了“where-ever或where”,其中包括引导,但没有这样做,你就不能使用Zend_Db_Table::getDefaultAdapter();内部引导。