PHP Swoole异步MySQL客户端实现方法

2020-01-1415:10:03后端程序开发Comments2,121 views字数 1056阅读模式

使用函数:swoole_mysql文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17674.html

使用版本:1.8.6及以上文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17674.html

把官方文档的例子改写成了面向对象的形式,示例如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17674.html

SwooleMysql.php:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17674.html

  1. <?php
  2. class SwooleMysql {
  3. public $db = "";
  4. public $server = [];
  5. public function __construct() {
  6. //实例化
  7. $this->db = new swoole_mysql();
  8. //配置参数
  9. $this->server = [
  10. 'host' => '127.0.0.1','port' => 3306,'user' => 'root','password' => 'xxooni','database' => 'lws','charset' => 'utf8','timeout' => 2,// 可选:连接超时时间(非查询超时时间),默认为SW_MYSQL_CONNECT_TIMEOUT(1.0)
  11. ];
  12. }
  13. //执行SQL语句操作
  14. public function execute($sql) {
  15. $this->db->connect($this->server,function($db,$res) use($sql) {
  16. echo "连接MySQL...\n";
  17. if($res === false) {
  18. var_dump($db->connect_errno,$db->connect_error);
  19. die;
  20. }
  21. $db->query($sql,$res){
  22. if($res === false) {
  23. var_dump($db->error,$db->errno);
  24. }elseif($result === true) {
  25. var_dump($db->affected_rows,$db->insert_id);
  26. }
  27. var_dump($res);
  28. $db->close();
  29. });
  30. });
  31. return true;
  32. }
  33. }
  34. $obj = new SwooleMysql();
  35. $sql = 'select `article_title` from `lws_article` where `article_id`=66';
  36. $res = $obj->execute($sql);
  37. var_dump($res);
  38. echo "lws\n";

运行结果:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17674.html

PHP Swoole异步MySQL客户端实现方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17674.html

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP扩展开发教程》、《PHP网络编程技巧总结》、《php curl用法总结》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》及《php字符串(string)用法总结》文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17674.html

希望本文所述对大家PHP程序设计有所帮助。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17674.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/17674.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定