美文网首页swoole
解决:PHP Fatal error: Uncaught Swo

解决:PHP Fatal error: Uncaught Swo

作者: 这真的是一个帅气的名字 | 来源:发表于2020-04-03 18:39 被阅读0次

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

    <?php
    
    class AysMysql{
    
        public $dbSource = "";
        public $dbConfig = [];
    
        public function __construct()
        {
            $this->dbSource = new Swoole\Coroutine\MySQL();
            $this->dbConfig = [
                'host' => '127.0.0.1',
                'port' => '3306',
                'user' => 'root',
                'password' => '123456',
                'database' => 'testSwo',
                'charset' => 'utf8'
            ];
    
        }
    
        /**
         * @Notes:mysql执行
         * @Interface execute
         * @param $id
         * @param $username
         * @return bool
         * @Time: 2020/4/3   下午5:48
         */
        public function execute($id,$username){
            go(function () use($id){
                //connect
                $this->dbSource->connect($this->dbConfig);
                $sql = "select * from user where id = ".$id;
                $res = $this->dbSource->query($sql);
                if($res === false){
                    var_dump("error");
                }
                var_dump($res);
                $this->dbSource->close();
            });
        }
    }
    
    $obj = new AysMysql();
    $obj->execute(2,'张三');
    
    image.png
    image.png

    相关文章

      网友评论

        本文标题:解决:PHP Fatal error: Uncaught Swo

        本文链接:https://www.haomeiwen.com/subject/jopkphtx.html