记录学习swoole--mysql的坑

一、PHP Fatal error: Uncaught Error: Class ‘swoole_mysql’ not found in xxx

这个错误是因为用了异步回调模块,swoole 4.3.0版本后已经移除了异步模块,所以会报错没有找到swoole_mysql模块,用Swoole\Mysql一样报错。
在这里插入图片描述
解决方案:
1、用Corotuine协程模块代替
2、回退到旧版本swoole(不推荐)

二、PHP Fatal error: Uncaught Swoole\Error: operation not support (reactor is not ready) in xxxxxx

报这个错是因为没有用协程方式运行脚本,而直接用了php运行,需要在代码外面套个go(function(){ …这里写内容},才能正常运行!

附上本人代码:
<?php

class Mymysql{

    // 数据库实例
    public $dbSource = '';
    // 数据库连接配置
    public $server = [];
    // 初始化参数
    public function __construct()
    {
        $this->dbSource = new Swoole\Coroutine\MySQL();
        $this->server = [
            'host' => '127.0.0.1',
            'port' => 3306,
            'user' => 'swoole',
            'password' => 'xxxxxxxxxxx',
            'database' => 'swoole',
            'charset' => 'utf8', //指定字符集
            'timeout' => 2,  // 可选:连接超时时间(非查询超时时间),默认为SW_MYSQL_CONNECT_TIMEOUT(1.0)
        ];
    }

    /**
     * 执行逻辑
     * @param $id
     * @param $username
     */
    public function execute(){
        // 连接
        go(function (){
            $this->dbSource->connect($this->server);
            $res = $this->dbSource->query('select * from test');
            if($res === false) {
                return;
            }
            var_dump($res);
            // 关闭连接
            $this->dbSource->close();
        });
    }
}

$obj = new Mymysql();
$obj->execute();

运行成功
运行成功图示

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值