Swoole provides a new asynchronous MySQL client in version 1.8.6. The bottom layer implements the MySQL communication protocol by itself without relying on other third-party libraries, such as libmysqlclient, mysqlnd, mysqli, etc. (Recommended learning: swoole video tutorial)
Starting from version 1.8.6, Swoole\MySQL has been built into Swoole and does not need to be turned on through the --enable-async-mysql compilation parameter.
swoole_mysql->__construct 创建异步mysql客户端。
Usage examples
$db = new swoole_mysql(); $server = array( 'host' => '192.168.56.102', 'port' => 3306, 'user' => 'test', 'password' => 'test', 'database' => 'test', 'charset' => 'utf8', //指定字符集 'timeout' => 2, // 可选:连接超时时间(非查询超时时间),默认为SW_MYSQL_CONNECT_TIMEOUT(1.0) ); $db->connect($server, function ($db, $r) { if ($r === false) { var_dump($db->connect_errno, $db->connect_error); die; } $sql = 'show tables'; $db->query($sql, function(swoole_mysql $db, $r) { if ($r === false) { var_dump($db->error, $db->errno); } elseif ($r === true ) { var_dump($db->affected_rows, $db->insert_id); } var_dump($r); $db->close(); }); });
The above is the detailed content of How to apply swoole_mysql. For more information, please follow other related articles on the PHP Chinese website!