您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-mysql組件-其它數據庫類=medoo的數據庫類的使用
发布时间:2021-12-23 22:02:33编辑:雪饮阅读()
为什么选择 Medoo
· 非常的轻量
未压缩只有 34KB。
· 简单
非常的容易学习,快速上手。
· 强大
支持各种常见的SQL查询。
· 兼容
支持各种数据:MySQL, MSSQL, SQLite, MariaDB, Oracle, Sybase, PostgreSQL等等
· 安全
防止SQL注入
· 免费
MIT 协议, 你可以进行任何修改。
在workerman中如何使用?
安裝medoo依賴
[root@localhost www.fpm.com]# /usr/local/php734/bin/php /usr/bin/composer require catfan/Medoo
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
Using version ^2.1 for catfan/medoo
./composer.json has been updated
Running composer update catfan/medoo
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking catfan/medoo (v2.1.4)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Downloading catfan/medoo (v2.1.4)
- Installing catfan/medoo (v2.1.4): Extracting archive
4 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!
然後實例代碼如:
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Medoo\Medoo;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('websocket://0.0.0.0:8484');
$worker->onWorkerStart = function($worker)
{
global $db;
// 初始化db连接
$db = new medoo([
'database_type' => 'mysql',
'database_name' => 'workerman',
'server' => '127.0.0.1',
'port'=>3306,
'username' => 'xy',
'password' => 'xy',
'charset' => 'utf8',
'prefix'=>'phome_'
]);
};
$worker->onMessage = function(TcpConnection $connection, $data)
{
global $db;
// 插入数据示例
$res=$db->insert('ecms_news', [
'classid' => 2208,
'ttid' => 8022,
'newspath' => 'snowDrink',
'smalltext' => ['en', 'fr', 'jp', 'cn']
]);
$response["res"]=$res;
$connection->send(json_encode($response));
};
Worker::runAll();
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Medoo\Medoo;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('websocket://0.0.0.0:8484');
$worker->onWorkerStart = function($worker)
{
global $db;
// 初始化db连接
$db = new medoo([
'database_type' => 'mysql',
'database_name' => 'workerman',
'server' => '127.0.0.1',
'port'=>3306,
'username' => 'xy',
'password' => 'xy',
'charset' => 'utf8',
'prefix'=>'phome_'
]);
};
$worker->onMessage = function(TcpConnection $connection, $data)
{
global $db;
// 插入数据示例
$res=$db->insert('ecms_news', [
'classid' => 2208,
'ttid' => 8022,
'newspath' => 'snowDrink',
'smalltext' => ['en', 'fr', 'jp', 'cn']
]);
$response["res"]=$res;
$connection->send(json_encode($response));
};
Worker::runAll();
這裏有一個不知道算不算是亮點的功能,就是說medoo某個字段插入數組就會自動序列化。
然後服務運行:
[root@localhost www.fpm.com]# /usr/local/php734/bin/php channelServer.php start
Workerman[channelServer.php] start in DEBUG mode
------------------------------------------- WORKERMAN --------------------------------------------
Workerman version:4.0.22 PHP version:7.3.4
-------------------------------------------- WORKERS ---------------------------------------------
proto user worker listen processes status
tcp root none websocket://0.0.0.0:8484 1 [OK]
--------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
若有websocket前端觸發上方onMessage事件:
同時若有sql追蹤器:
关键字词:workerman,mysql,組件,其它數據庫類,medoo
相关文章
- workerman-mysql组件-其它数据库类使用-thinkphp(thin
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm