报这个错是因为没有用协程方式运行脚本,而直接用了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
网友评论