0
votes

I am a newbie just start learning Zend Framework 2. When I am following the tutorial http://framework.zend.com/manual/2.3/en/user-guide/database-and-models.html, I have a problem with find database.

I found that it can connect to the localhost database, but cannot find any database.

Connect Error: SQLSTATE[HY000] [1049] Unknown database 'db_vote'

It can only find the database "mysql". But cannot find the new table if I create a new table inside "mysql".

The other thing weird is I changed my root password in phpmyadmin, but in zf2, if I change to new password it said access denied, but it works with the old password.

Seems like zend is connecting to the other "localhost"? Weird thoughts... Anyone can help me? Thanks in advance.

Code in Module.php:

public function getServiceConfig(){
    return array(
        'factories' => array(
            'Album\Model\AlbumTable' => function($sm){
              $tableGateway = $sm->get('AlbumTableGateway');
              $table = new AlbumTable($tableGateway);
              return $table;  
            },
            'AlbumTableGateway' => function($sm){
              $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
              $resultSetPrototype = new ResultSet();
              $resultSetPrototype->setArrayObjectPrototype(new Album());
              return new TableGateway('album_zend', $dbAdapter, null, $resultSetPrototype); 
            },
        ),
    );
}

global.php:

return array(
'db' => array(
    'driver'        => 'Pdo',
    'dsn'           => 'mysql:dbname=db_vote; 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:

return array(
'db' => array(
    'username' => 'root',
    'password' => 'xxxxxxx',
),
);
1
so where is your settings config file? where is your code?Alex
Did you create a database called db_vote? You should be able to do this with phpmyadminTim Fountain
hi Tim, yes I create a database called db_vote. I even tried every database in localhost, its all showing unknown database. Only mysql can be reachedkim
hi Alex, I attached the related codekim
Do you have just one db server on that machine? "Seems like zend is connecting to the other "localhost"?" - what do you mean by that?Michal M.

1 Answers

0
votes

Problem:

root@localhost

root@%

can be two different users.

If you look into user table in mysql database you will notice more than one root user. User for the web app might be different from the one you used to connect though the console.

How to investigate it:

Check which user is connected through the console (or any other tool you have used):

SELECT USER();

Make sure that you either stick to one user (root@localhost for instance) or make sure that the web app user has the right privileges:

USE mysql;
SELECT * FROM db;

Whenever you change privileges don't forget to flush them:

FLUSH PRIVILEGES;