新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
yii2連接數(shù)據(jù)庫失敗怎么辦-創(chuàng)新互聯(lián)
這篇文章主要介紹yii2連接數(shù)據(jù)庫失敗怎么辦,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
首先,先來看一下問題代碼:
1、控制器代碼如下:
public function actionIndex() { $query = Country::find(); $pagination = new Pagination([ 'defaultPageSize' => 5, 'totalCount' => $query->count() ]); $countries = $query->orderBy('name') ->offset($pagination->offset) ->limit($pagination->limit) ->all(); return $this->render('index', [ 'countries' => $countries, 'pagination' => $pagination, ]); }
(推薦教程:yii框架)
2、數(shù)據(jù)庫配置文件db.php代碼如下:
'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=yii2basic', 'username' => 'root', 'password' => '', 'charset' => 'utf8', // Schema cache options (for production environment) //'enableSchemaCache' => true, //'schemaCacheDuration' => 60, //'schemaCache' => 'cache', ];
解決方法:
將PDO連接中的dsn的host由“l(fā)ocalhost”改為“127.0.0.1”即可,打開文件DB.PHP,修改如下:
'yii\db\Connection', 'dsn' => 'mysql:host=127.0.0.1;dbname=yii2basic', 'username' => 'root', 'password' => '', 'charset' => 'utf8', // Schema cache options (for production environment) //'enableSchemaCache' => true, //'schemaCacheDuration' => 60, //'schemaCache' => 'cache', ];
以上是“yii2連接數(shù)據(jù)庫失敗怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
名稱欄目:yii2連接數(shù)據(jù)庫失敗怎么辦-創(chuàng)新互聯(lián)
瀏覽地址:http://www.dlmjj.cn/article/djpisp.html