作者:手机用户2502858383_827 | 来源:互联网 | 2023-08-23 18:31
使用函数:swoole_mysql
使用版本:1.8.6及以上
把官方文档的例子改写成了面向对象的形式,示例如下:
SwooleMysql.php:
db = new swoole_mysql();//配置参数$this->server = ['host' => '127.0.0.1','port' => 3306,'user' => 'root','password' => 'xxooni','database' => 'lws','charset' => 'utf8','timeout' => 2, // 可选:连接超时时间(非查询超时时间),默认为SW_MYSQL_CONNECT_TIMEOUT(1.0)];}//执行SQL语句操作public function execute($sql) {$this->db->connect($this->server, function($db, $res) use($sql) {echo "连接MySQL...\n";if($res === false) {var_dump($db->connect_errno, $db->connect_error);die;}$db->query($sql, function($db, $res){if($res === false) {var_dump($db->error, $db->errno);}elseif($result === true) {var_dump($db->affected_rows, $db->insert_id);}var_dump($res);$db->close();});});return true;}
}$obj = new SwooleMysql();
$sql = 'select `article_title` from `lws_article` where `article_id`=66';
$res = $obj->execute($sql);
var_dump($res);echo "lws\n";
运行结果: