1.修改配置文件/.nev
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、成都做網(wǎng)站、華寧網(wǎng)絡(luò)推廣、小程序開發(fā)、華寧網(wǎng)絡(luò)營銷、華寧企業(yè)策劃、華寧品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供華寧建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
DB_HOST=127.0.0.1 //數(shù)據(jù)庫地址 DB_PORT=3306 //數(shù)據(jù)庫端口 DB_DATABASE=database //數(shù)據(jù)庫名稱 DB_USERNAME=root //數(shù)據(jù)庫賬號 DB_PASSWORD=root //數(shù)據(jù)庫密碼
2.開始測試是否連接成功
/App/Http/routes.php 路由代碼如下(可以自定義)
Route::get('dbtest','Admin\IndexController@dbtest');
3.新建IndexController控制器
/App/Http/Controllers/IndexController 控制器代碼如下(可以自定義)
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.如果你的數(shù)據(jù)庫表有前綴怎么處理
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 //數(shù)據(jù)庫地址 DB_PORT=3306 //數(shù)據(jù)庫端口 DB_DATABASE=database //數(shù)據(jù)庫名稱 DB_PREFIX=unics_ //數(shù)據(jù)庫表前綴名稱 DB_USERNAME=root //數(shù)據(jù)庫賬號 DB_PASSWORD=root //數(shù)據(jù)庫密碼