我正在尝试在运行时连接数据库,我正在从会话中设置连接设置,如下所示。第一个连接(weserach)成功但是会话变量(wesearch_barcode)的连接不是。
这是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,
],
这是我的控制器:
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;
}
}
}
}
这是我的central_db模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class central_db extends Model
{
protected $connection ='wesearch';
protected $table = 'usr_waev';
}
这是另一个模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class barcodedb extends Model
{
protected $connection ='wesearch_barcodedb';
protected $table = 'user';
}
错误是这样的 " Connector.php第119行中的PDOException: SQLSTATE [HY000] [1045]拒绝用户访问'' 139.59.29.54' (使用密码:否)"