您好,登錄后才能下訂單哦!
1.修改配置文件/.nev
DB_HOST=127.0.0.1 //數據庫地址 DB_PORT=3306 //數據庫端口 DB_DATABASE=database //數據庫名稱 DB_USERNAME=root //數據庫賬號 DB_PASSWORD=root //數據庫密碼
2.開始測試是否連接成功
/App/Http/routes.php 路由代碼如下(可以自定義)
Route::get('dbtest','Admin\IndexController@dbtest');
3.新建IndexController控制器
/App/Http/Controllers/IndexController 控制器代碼如下(可以自定義)
<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\DB; class IndexController extends Controller{ public function dbcc(){ $pdo=DB::connection()->getPdo(); dd($pdo); } }
4.瀏覽訪問測試
瀏覽器訪問http://127.0.0.1/index.php/dbtest 成功提示如下圖
PDO {#123 ▼ inTransaction: false attributes: {▼ CASE: NATURAL ERRMODE: EXCEPTION AUTOCOMMIT: 1 PERSISTENT: false DRIVER_NAME: "mysql" SERVER_INFO: "Uptime: 3026 Threads: 4 Questions: 476 Slow queries: 0 Opens: 42 Flush tables: 1 Open tables: 1 Queries per second avg: 0.157" ORACLE_NULLS: NATURAL CLIENT_VERSION: "mysqlnd 5.0.11-dev - 20120503 - $Id: f373ea5dd5538761406a8022a4b8a374418b240e $" SERVER_VERSION: "5.5.40" STATEMENT_CLASS: array:1 [] CONNECTION_STATUS: "127.0.0.1 via TCP/IP" DEFAULT_FETCH_MODE: BOTH }}
遇到常見問題;
1.如果你的數據庫表有前綴怎么處理
1.1/config/database.php找打如下代碼
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'strict' => false, 'engine' => null, ],
1.2修改成如下代碼
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'prefix' => env('DB_PREFIX', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'strict' => false, 'engine' => null, ],
1.3配置文件/.nev修改成如下代碼
DB_HOST=127.0.0.1 //數據庫地址 DB_PORT=3306 //數據庫端口 DB_DATABASE=database //數據庫名稱 DB_PREFIX=unics_ //數據庫表前綴名稱 DB_USERNAME=root //數據庫賬號 DB_PASSWORD=root //數據庫密碼
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。