0

I've eventually made my connection with mysql instead of mssql because it was not working (I'm working with laravel). But now it's still not working. I receive the error:

SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected (SQL: create table `users` (`id` int unsigned not null auto_increment primary key, `username` varchar(32) not null, `email` varchar(320) not null, `password` varchar(60) not null, `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci)

I'm connection to my localhost with a wamp server. This is my code:

> 'mysql' => [
>             'driver'    => 'mysql',
>             'host'      => env('localhost'),
>             'database'  => env('test'),
>             'username'  => env('root'),
>             'password'  => env(''),
>             'charset'   => 'utf8',
>             'collation' => 'utf8_unicode_ci',
>             'prefix'    => '',
>             'strict'    => false,
>         ],

And my env :

'default' => env('mysql', 'mysql'),

EDIT

.env:

DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=
4
  • Are you using Laravel 5? Commented Jul 10, 2015 at 17:46
  • Did you set the database as well as the rest of the information inside your .env file? Commented Jul 10, 2015 at 18:10
  • Yes I did please see my edit. Very strange what am I doing wrong? Commented Jul 11, 2015 at 15:53
  • It should be env('DB_HOST'), etc. Commented Jul 11, 2015 at 16:07

2 Answers 2

1

syntax env('key','default') with:

key is key before = in env file

default is default value if key not exist in env file

please edit as follows:

> 'mysql' => [
>             'driver'    => 'mysql',
>             'host'      => env('DB_HOST', 'localhost'),
>             'database'  => env('DB_DATABASE','db_name'),
>             'username'  => env('DB_USERNAME','root'),
>             'password'  => env('DB_PASSWORD',''),
>             'charset'   => 'utf8',
>             'collation' => 'utf8_unicode_ci',
>             'prefix'    => '',
>             'strict'    => false,
>         ],

P/s: excuse me my English.

Sign up to request clarification or add additional context in comments.

Comments

0

.env file

DB_HOST=localhost
DB_DATABASE=dbname
DB_USERNAME=dbusername
DB_PASSWORD=dbpassword

config/database.php

 'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'databasename'),
            'username'  => env('DB_USERNAME', 'dbusername'),
            'password'  => env('DB_PASSWORD', 'dbpassword'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

As you can see in the database.php env('DB_DATABASE', 'databasename'), means, check for DB_DATABASE value in .env file, and if its not found or set, use databasename that you provide as the parameter.

In your case, the syntax of env was screwed up, which probably caused the error. Actually, if you have a .env file, you dont have to edit it at all. Or you can avoid using an .env file, and provide the datas in the database.php in the said syntax, not the one you are using at the moment.

Note : .env file has the first priority, and if you use mention database1 in .env file and mention database2 in the database.php file, database1 will be used.

Comments

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.