注意!任何swoole函数都应该包含在go(function(){})
- 经典查询方法query()
go(function (){
$swoole_mysql = new Swoole\Coroutine\MySQL();
$swoole_mysql->connect([
'host' => '127.0.0.1',
'port' => 3306,
'user' => 'root',
'password' => 'xxxx',
'database' => 'xxxxdb',
]);
$res = $swoole_mysql->query('select * from colname');
if($res === false) {
return;
}
foreach ($res as $value) {
echo $value['comment'].PHP_EOL;
}
});
知识兔- 增强型---防止SQL注入攻击的方法
<?php
use Swoole\Coroutine as co;
co::create(function (){
$db = new co\MySQL();
$config=array(
'host'=>'localhost',
'database'=>'xxxxdb',
'user'=>'root',
'password'=>'xxxx',
'fetch_mode' => true,
);
$db->connect($config);
$stmt = $db->prepare("select * from colname");
$stmt->execute();
$res=$stmt->fetchAll();
var_dump($res);
});
知识兔- 增强型---带参数的查询样例
<?php
use Swoole\Coroutine as co;
co::create(function (){
$db = new co\MySQL();
$config=array(
'host'=>'localhost',
'user'=>'root',
'database'=>'xxxxdb',
'password'=>'xxxx',
'fetch_mode'=>true,
);
$db->connect($config);
$stmt = $db->prepare('select * from colname where tblbelongs=? and bz=?');
var_dump($stmt);
$stmt->execute(array('incomedoc_mx','s'));
var_dump($stmt->fetchAll());
});
知识兔