使用Zend \ Db连接到DB

时间:2012-12-30 15:46:46

标签: php zend-framework2

我正在尝试在我的应用程序中实现Zend Db。

目前,我正在与会话管理员合作。

Db是这样发起的:

use Zend\Config\Config;
$config = new Zend\Config\Config(include ROOT . DS . 'config'  . DS . 'config.php');
$database = new Zend\Db\Adapter\Adapter($config->database->toArray());

虽然我的配置文件如下所示:

return array(
'sie_product_name'  => 'product name',
'salt' => 'salt',
'max_session_time' => '3600',
'database' => array(
    'driver' => 'MYSQLI',
    'params'  => array(
        'host'     => 'localhost',
        'username' => 'username',
        'password' => 'password',
        'dbname'   => 'dbname'
    )
)
);

我正在尝试执行选择:

$sql = new Sql($this->database);
$query = $sql->select();
$query->from('sessions');
$query->columns(array('sessionid'));
$query->where(array('sessionid' => $sessionId));

$stmt = $sql->prepareStatementForSqlObject($query);
$result = $stmt->execute();

我收到错误:

Connect Error: SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)

很酷,这是一个连接错误......但是连接丢失的地方,或者我应该说,凭据在哪里丢失了?我们来看看数据库对象......

Zend\Db\Adapter\Adapter Object
(
[driver:protected] => Zend\Db\Adapter\Driver\Mysqli\Mysqli Object
    (
        [connection:protected] => Zend\Db\Adapter\Driver\Mysqli\Connection Object
            (
                [driver:protected] => Zend\Db\Adapter\Driver\Mysqli\Mysqli Object
 *RECURSION*
                [connectionParameters:protected] => Array
                    (
                        [driver] => MYSQLI
                        [params] => Array
                            (
                                [host] => localhost
                                [username] => username
                                [password] => password
                                [dbname] => dbname
                            )

                    )

                [resource:protected] => mysqli Object
                    (
                        [affected_rows] => 
                        [client_info] => mysqlnd 5.0.10 - 20111026 - $Id: b0b3b15c693b7f6aeb3aa66b646fee339f175e39 $
                        [client_version] => 50010
                        [connect_errno] => 1045
                        [connect_error] => Access denied for user ''@'localhost' (using password: NO)
                        [errno] => 1045
                        [error] => Access denied for user ''@'localhost' (using password: NO)
                        [error_list] => 
                        [field_count] => 
                        [host_info] => 
                        [info] => 
                        [insert_id] => 
                        [server_info] => 
                        [server_version] => 
                        [stat] => 
                        [sqlstate] => 
                        [protocol_version] => 
                        [thread_id] => 
                        [warning_count] => 
                    )

                [inTransaction:protected] => 
            )

        [statementPrototype:protected] => Zend\Db\Adapter\Driver\Mysqli\Statement Object
            (
                [mysqli:protected] => 
                [driver:protected] => Zend\Db\Adapter\Driver\Mysqli\Mysqli Object
 *RECURSION*
                [sql:protected] => 
                [parameterContainer:protected] => 
                [resource:protected] => 
                [isPrepared:protected] => 
                [bufferResults:protected] => 
            )

        [resultPrototype:protected] => Zend\Db\Adapter\Driver\Mysqli\Result Object
            (
                [resource:protected] => 
                [isBuffered:protected] => 
                [position:protected] => 0
                [numberOfRows:protected] => -1
                [currentComplete:protected] => 
                [nextComplete:protected] => 
                [currentData:protected] => 
                [statementBindValues:protected] => Array
                    (
                        [keys] => 
                        [values] => Array
                            (
                            )

                    )

                [generatedValue:protected] => 
            )

        [options:protected] => Array
            (
                [buffer_results] => 
            )

    )

[platform:protected] => Zend\Db\Adapter\Platform\Mysql Object
    (
    )

[queryResultSetPrototype:protected] => Zend\Db\ResultSet\ResultSet Object
    (
        [allowedReturnTypes:protected] => Array
            (
                [0] => arrayobject
                [1] => array
            )

        [arrayObjectPrototype:protected] => ArrayObject Object
            (
                [storage:ArrayObject:private] => Array
                    (
                    )

            )

        [returnType:protected] => arrayobject
        [buffer:protected] => 
        [count:protected] => 
        [dataSource:protected] => 
        [fieldCount:protected] => 
        [position:protected] => 
    )

[lastPreparedStatement:protected] => 
)

看起来正在设置连接参数 - 太棒了!但它仍然没有连接。

1 个答案:

答案 0 :(得分:1)

在ZF 2中,您不需要名为params的键:

http://framework.zend.com/manual/2.0/en/modules/zend.db.adapter.html

$adapter = new Zend\Db\Adapter\Adapter(array(
    'driver'   => 'Mysqli',
    'database' => 'database',
    'username' => 'username',
    'password' => 'password'
));