yii链接postgresql
Posted Iven_Han
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yii链接postgresql相关的知识,希望对你有一定的参考价值。
ubuntu
官网介绍http://php.net/manual/en/ref.pdo-pgsql.php
具体步骤:
wget http://pecl.php.net/get/PDO_PGSQL-1.0.2.tgz
tar -zxvf PDO_PGSQL-1.0.2.tgz
cd /Download/PDO_PGSQL-1.0.2/PDO_PGSQL-1.0.2
(如果不清楚php安装在哪儿,最简单的办法,直接输入命令: whereis phpize, 我的文件目录是在/usr/bin/phpize)
usr/bin/phpize
./configure --with-pdo-pgsql
(感谢dahuzix让我找到了./configure的路径 http://blog.csdn.net/dahuzix/article/details/76283871, 不过不需要全部按照他的步骤安装)
此时pdo_pgsql.so已经安装好了, 下面进行配置文件的配置
sudo vi /etc/php/7.1/cli/php.ini (因为我是使用shell脚本运行的 php yii serve, 所以在cli目录下的php.ini。使用其他方法在文章末尾有相对应的路径)
(etc文件夹是配置文件夹, 所有应用运行都先经过这里查看配置, 所以别到其他地方配置php.ini)
在指令模式下输入 /pgsql 即可跳到extension部分,添加下面代码
extension=pdo.so
extension=pdo_pgsql.so
此时运行部分的配置已经好了, 下面就是yii部分去访问数据库
在yii项目的config目录下的db.php文件中对数据库进行配置
return [ ‘class‘ => ‘yii\db\Connection‘, ‘dsn‘ => ‘pgsql:host=localhost;port=5432;dbname=exampledb‘, ‘username‘ => ‘dbuser‘, ‘password‘ => ‘abc123_‘, ‘charset‘ => ‘utf8‘, ];
dbname是你的数据库名
在controller中创建TestController.php文件, 创建访问Test测试
public function actionTest() { $test = Yii::$app->db->createCommand("UPDATE user_tbl SET name = ‘ttt‘ WHERE id = 9999991"); return $test->execute(); }
测试方法用来证明链接数据库正确, 其他修改数据库的操作自己研究
cd 到你的项目目录, 运行php yii serve
在浏览器访问方法: http://localhost:8080/controllers/test/test
controllers是你的Controller所在目录,test问TestController.php, test是actionTest()方法
以上是关于yii链接postgresql的主要内容,如果未能解决你的问题,请参考以下文章