0

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 Answers 2

1

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',
     ),
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Erik I'll try this and let you know. (FYI: I won't check immediately but as soon I do I'll inform you)
0

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" :)

3 Comments

Thank you for this I'll look into that but could you please confirm in my code factories setting path is correct as I mentioned above?
because when I tried to connect via application.ini it's working fine. I really want to know what mistake I'm doing here?
I checked the log files you mentioned but only the access.log has some data for accessing that URL other than that there is no data in php error log or mysql error log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.