jeudi 29 octobre 2015

multiple database connection not working laravel

I am trying to make use of two database in application.

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

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

I have removed the connection parameters that exists in .env. Now my first connection is working properly. I can make use of the first database very fluently. But when i try to get data from the table of another database, I get SQLSTATE[HY000] [1045] Access denied for user 'xxxxxxx'@'localhost' (using password: YES). But the multiple connection is working fine in my localhost. This is how i am trying to get data from the second database

Route::post('params', function(){

    $posted = Input::get('imei');

    $user2 = DB::connection('mysql2');
    $u = $user2->table('tablename')->where('fld_imei', '=', $posted)->first();

    if (count($u) == '0'){
        echo "there are no records matching this serial number";
        die();
    }

    return view('result')
        ->with(array(
            'result'    => $u
        ));

});

How can I make this work?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire