0
votes

I'm sorry I'm completely new to Zend Framework 2 with some tutorials I'm trying to connect my DB connection as follows,

Created a file in xampp\htdocs\articlemanager\application\configs\autoload\global.php

Inserted the following Zend DB connection code to global.php

<?php
return array(
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
        'aliases' => array(
            'db' => 'Zend\Db\Adapter\Adapter',
        ),
    ),
    'db' => array(
        'driver'    => 'PDO_MYSQL',
        'dsn'       => 'mysql:dbname=articlemanager;host=localhost',
        'username'  => 'root',
        'password'  => '',
        'driver_options' => array(
                        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
         ),
    ),
);

and In the Indexcontroller (\xampp\htdocs\articlemanager\application\controllers\IndexController.php) tested adding $this->db = $this->getServiceLocator()->get('db'); in the indexAction as follows

public function indexAction()
    {
        $this->db = $this->getServiceLocator()->get('db');
    }

When I refresh page it display as

An error occurred

Application error

Can I know what I missed here?

Also I would like to know My Zend Library is in the \xampp\php\Zend and My global.php file in the xampp\htdocs\articlemanager\application\configs\autoload\global.php is it OK?

2

2 Answers

1
votes

Why are you using an Alias on 'Db' ?

Try this, your driver name is different from mine.

In addition, please move your username and password to local.php.. so they do not persist in Git projects

global.php

<?php
return array(
    'db' => array(
        'driver'    => 'Pdo',
        'dsn'       => 'mysql:dbname=articlemanager;host=localhost',
        'driver_options' => array(
                        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
         ),
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
);

local.php example

<?php
return array(
    'db' => array(
        'username' => 'DB_USERNAME',
        'password' => 'DB_PASSWORD',
     ),
);
0
votes

database connection problems can be a number of different issues that are not necessarily obvious from looking at the creds / db config info.

have you taken a look at any logs? my application has several different logs setup - PHP error, access log, mysql error log, etc. - depending on your application, you may not have this many as discretely defined, but checking any logs you do have will give you a whole lot more information than just "An error has occurred" :)