您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman常用组件-MySQL组件-workerman之使用Workerman-MySQL - 具体MySQL的Connection用法-獲取所有數據
发布时间:2021-12-21 22:38:06编辑:雪饮阅读()
具體實例:
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('websocket://0.0.0.0:8484');
$worker->onWorkerStart = function($worker)
{
global $db;
// 初始化db连接
$db = new \Workerman\MySQL\Connection('127.0.0.1', '3306', 'xy', 'xy', 'workerman');
};
$worker->onMessage = function(TcpConnection $connection, $data)
{
global $db;
$response=[];
// 获取所有数据
$response["all"]=$db->select('id,classid')->from('phome_ecms_news_data_1')->where('id>:id and classid<>:classid')->bindValues(array('id'=>82, 'classid' => 83))->query();
//等价于
$response["all2"]=$db->select('id,classid')->from('phome_ecms_news_data_1')->where("id>82 and classid <> 83")->query();
//等价于
$response["all3"]=$db->query("SELECT ID,classid FROM `phome_ecms_news_data_1` WHERE id>82 and classid <> 83");
//還可以用字符串查詢
$response["all4"]=$db->select('id,classid')->from('phome_ecms_news_data_1')->where('id>:id and newstext=:newstext')->bindValues(array('id'=>82, 'newstext' => "雪饮"))->query();
$response["all5"]=$db->select('id,classid')->from('phome_ecms_news_data_1')->where("id>82 and newstext='雪饮'")->query();
$response["all6"]=$db->query("SELECT ID,classid FROM `phome_ecms_news_data_1` WHERE id>82 and newstext='雪饮'");
$connection->send(json_encode($response));
};
// 运行worker
Worker::runAll();
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('websocket://0.0.0.0:8484');
$worker->onWorkerStart = function($worker)
{
global $db;
// 初始化db连接
$db = new \Workerman\MySQL\Connection('127.0.0.1', '3306', 'xy', 'xy', 'workerman');
};
$worker->onMessage = function(TcpConnection $connection, $data)
{
global $db;
$response=[];
// 获取所有数据
$response["all"]=$db->select('id,classid')->from('phome_ecms_news_data_1')->where('id>:id and classid<>:classid')->bindValues(array('id'=>82, 'classid' => 83))->query();
//等价于
$response["all2"]=$db->select('id,classid')->from('phome_ecms_news_data_1')->where("id>82 and classid <> 83")->query();
//等价于
$response["all3"]=$db->query("SELECT ID,classid FROM `phome_ecms_news_data_1` WHERE id>82 and classid <> 83");
//還可以用字符串查詢
$response["all4"]=$db->select('id,classid')->from('phome_ecms_news_data_1')->where('id>:id and newstext=:newstext')->bindValues(array('id'=>82, 'newstext' => "雪饮"))->query();
$response["all5"]=$db->select('id,classid')->from('phome_ecms_news_data_1')->where("id>82 and newstext='雪饮'")->query();
$response["all6"]=$db->query("SELECT ID,classid FROM `phome_ecms_news_data_1` WHERE id>82 and newstext='雪饮'");
$connection->send(json_encode($response));
};
// 运行worker
Worker::runAll();
前端脚本還是用上次的前端脚本好了
服務端運行:
[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.
前端訪問:
关键字词:workerman,mysql,Connection
上一篇:workerman常用组件-MySQL组件-workerman之使用Workerman-MySQL
下一篇:workerman常用组件-MySQL组件-workerman之使用Workerman-MySQL - 具体MySQL的Connection用法-獲取一行數據
相关文章
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之安装 Worker
- workerman常用组件-MySQL组件-workerman之mysql-pdo_m
- workerman常用组件-FileMonitor文件监控组件
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelSer