无法连接到数据库 - 没有数据库选择错误

时间:2014-02-18 11:27:08

标签: php mysql pdo laravel

我一直在努力使这项工作持续一小时左右。

当我尝试在laravel中执行查询时,我收到错误。

确切错误如下:

exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in D:\www\testapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:272
Stack trace:
#0 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php(272): PDO->prepare('select count(*)...')
#1 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php(521): Illuminate\Database\Connection->Illuminate\Database\{closure}(Object(Illuminate\Database\MySqlConnection), 'select count(*)...', Array)
#2 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php(277): Illuminate\Database\Connection->run('select count(*)...', Array, Object(Closure))
#3 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(1028): Illuminate\Database\Connection->select('select count(*)...', Array)
#4 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(1018): Illuminate\Database\Query\Builder->runSelect()
#5 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(1005): Illuminate\Database\Query\Builder->getFresh(Array)
#6 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(1369): Illuminate\Database\Query\Builder->get(Array)
#7 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(1311): Illuminate\Database\Query\Builder->aggregate('count', Array)
#8 [internal function]: Illuminate\Database\Query\Builder->count()
#9 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(786): call_user_func_array(Array, Array)
#10 D:\www\testapp\app\routes.php(67): Illuminate\Database\Eloquent\Builder->__call('count', Array)
#11 D:\www\testapp\app\routes.php(67): Illuminate\Database\Eloquent\Builder->count()
#12 [internal function]: {closure}()
#13 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Routing\Route.php(80): call_user_func_array(Object(Closure), Array)
#14 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Routing\Route.php(47): Illuminate\Routing\Route->callCallable()
#15 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Routing\Router.php(1016): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#16 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(574): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#17 D:\www\testapp\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(550): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#18 D:\www\testapp\public\index.php(50): Illuminate\Foundation\Application->run()
#19 {main}

我的config / database.php如下:

'connections' => array(

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'testdatabase',
        'username'  => 'MYUSER',
        'password'  => 'MYPASSWORD',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

),

触发此错误的代码是:

 $users = DB::table('users')->get();
 print_r($users . "users");

有人可以请求帮助,我检查了给定MySQL用户的权限,我也尝试使用root帐户,但到目前为止无济于事。

1 个答案:

答案 0 :(得分:1)

这是一个环境问题。

database.php配置文件放入config/{environment}/database.php,然后检查bootstrap/start.php文件中是否正确定义了env,它应该可以正常工作。

要检查您当前的环境是什么,请运行:

echo App::environment();