0

I am trying to connect with database on runtime in which i am setting connection settings from session like following. first connection (weserach) was successful but the connection from session variable(wesearch_barcode) wasn't.

this is database.php

'wesearch' => [ 
            'driver' => 'mysql', 
            'host' => 'waev.in', 
            'port' => env('DB_PORT', '3306'), 
            'database' => 'wesearch_waev_user', 
            'username' => '******', 
            'password' => '******', 
            'charset' => 'latin1', 
            'collation' => 'latin1_swedish_ci', 
            'prefix' => '', 
            'strict' => true, 
            'engine' => null, 
            ],

        'wesearch_barcodedb' => [ 
            'driver' => 'mysql', 
            'host' => 'waev.in', 
            'port' => env('DB_PORT', '3306'), 
            'database' => $_SESSION['dbname'], 
            'username' => $_SESSION['username'], 
            'password' => $_SESSION['password'], 
            'charset' => 'latin1', 
            'collation' => 'latin1_swedish_ci', 
            'prefix' => '', 
            'strict' => true, 
            'engine' => null, 
            ],

this is my controller:

namespace App\Http\Controllers;
session_start();

use Illuminate\Http\Request;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Facades\DB;

use App\Http\Requests;
use App\central_db;
use App\barcodedb;

class check_subsController extends Controller
{
    public function fetch(Request $request){



        if($db=\App\central_db::select(['db_name', 'password', 'first_name', 'last_name'])->where('device_srno','=',$request->srno)->get()){

            foreach ($db as $record) {
                $_SESSION['dbname'] = $record->db_name;
                $_SESSION['username'] = $record->last_name;
                $_SESSION['password'] = $record->first_name;

            }
            if($users=\App\barcodedb::where('park','=','1')->get()){

                return $users;
            }
        }

    }
}

This is my central_db model:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;


class central_db extends Model
{
    protected $connection ='wesearch';
    protected $table = 'usr_waev';
}

And this is another one model:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;


class barcodedb extends Model
{
    protected $connection ='wesearch_barcodedb';
    protected $table = 'user';
}

And the error is something like this "PDOException in Connector.php line 119: SQLSTATE[HY000] [1045] Access denied for user ''@'139.59.29.54' (using password: NO)"

SamD
  • 185
  • 5
  • 22
  • This means that you're connecting to a database with an incorrect username and password. It's better practise to set database connection values in the `.env` file. Also i'm not sure if it's possible to set database connection values from a session. I don't know why you even want to use a `SESSION variable` there. – Lars Mertens Oct 11 '16 at 12:46
  • because i am fetching database credentials from first database connection and later want to connect with fetched database. is there any other way to do it ?? – SamD Oct 11 '16 at 12:53
  • 1
    This thread might be usefull for you: http://stackoverflow.com/questions/31847054/how-to-use-multiple-database-in-laravel – Lars Mertens Oct 11 '16 at 12:59
  • yeah but how to set connection properties in database.php at runtime. like i want to set database name, username, password on runtime. – SamD Oct 11 '16 at 13:13
  • 1
    I wouldn't recommend but here is how: http://stackoverflow.com/questions/31041893/laravel-change-database-connection-run-time – Lars Mertens Oct 11 '16 at 13:16
  • This looks like flawed design. Why are you fetching database credentials from a database in the first place? – Elias Oct 11 '16 at 13:33
  • because there are databases for my each client and all database credentials are stored in one another database. – SamD Oct 11 '16 at 13:50
  • you have any other idea?? – SamD Oct 11 '16 at 13:51
  • @sam by default laravel uses its own session driver (not phps). so use `session([$key => $value])` and `session($key)` [Laravel-Session](https://laravel.com/docs/5.3/session#using-the-session) – Fiete Oct 11 '16 at 13:58

0 Answers0