您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
webman-配置數據庫-webman中postgresql的配置使用
发布时间:2022-01-31 22:39:34编辑:雪饮阅读()
上篇中安裝了php7.3.4的pdo_pgsql擴展后,那麽接下來就能在webman中使用postgresql了,以簡單的讀取爲例,以查詢之前那個user表爲例。
首先config/database.php配置如:
<?php
return [
// 默认数据库
'default' => 'mysql',
// 各种数据库配置
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'database' => 'workerman',
'username' => 'xy',
'password' => 'xy',
'unix_socket' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'phome_',
'strict' => true,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => '192.168.1.3',
'port' => 5432,
'database' => 'webman',
'username' => 'postgres',
'password' => 'xy',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
],
];
然後控制器app\controller\Index.php編碼如:
<?php
namespace app\controller;
use support\Request;
use support\Db;
class Index
{
public function index(Request $request)
{
$users = Db::connection('pgsql')->select("select * from public.user");
return response(var_export($users,true));
}
public function view(Request $request)
{
return view('index/view', ['name' => 'webman']);
}
public function json(Request $request)
{
return json(['code' => 0, 'msg' => 'ok']);
}
}
實例運行起來,然後有被訪問到后的響應如:
[root@localhost pdo_pgsql]# elinks http://127.0.0.1:8787/Index/index --dump
array ( 0 => (object) array( 'id' => 1, 'name' => '雪飲', ), )
关键字词:webman,配置,數據庫,postgresql,使用