bear
bear

Reputation: 11605

Connecting to the DB using Zend\Db

I'm trying to implement Zend Db in my application.

Currently, I'm working with a session manager.

Db is initiated as such:

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());

Whilst my config file looks like this:

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'
    )
)
);

Where I'm trying to perform a select:

$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();

I am getting error:

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

Cool, it's a connection error... but where is the connection getting lost, or should I say, where are the credentials getting lost? Let's look at the database object...

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] => 
)

Looks like the connection parameters are being set - great! But it's still not connecting.

Upvotes: 1

Views: 1026

Answers (1)

cmbuckley
cmbuckley

Reputation: 42458

In ZF 2, you do not need a key named 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'
));

Upvotes: 1

Related Questions