您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

yii2连接数据库失败

2026/2/18 17:10:45发布15次查看
首先,先来看一下问题代码:
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、数据库配置文件db.php代码如下:
<?php return [ 'class' => '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由“localhost”改为“127.0.0.1”即可,打开文件db.php,修改如下:
<?php return [ 'class' => '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连接数据库失败的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product