ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
# Coroutine\\MySQL->prepare [TOC] 向`MySQL`服务器发送`SQL`预处理请求。`prepare`必须与`execute`配合使用。预处理请求成功后,调用`execute`方法向`MySQL`服务器发送数据参数。 ~~~ function Coroutine\MySQL->prepare(string $sql, float $timeout) : bool ~~~ > 需要`2.0.11`或更高版本 ## 参数 * `$sql`预处理语句,使用`?`作为参数占位符 * `$timeout`超时时间 ## 返回值 * 失败返回`false`,可检查`$db->error`和`$db->errno`判断错误原因 * 成功返回`Coroutine\MySQL\Statement`对象,可调用对象的`execute`方法发送参数 ## 示例 ~~~ use Swoole\Coroutine as co; co::create(function() { $db = new co\MySQL(); $server = array( 'host' => '127.0.0.1', 'user' => 'root', 'password' => 'root', 'database' => 'test', ); $ret1 = $db->connect($server); $stmt = $db->prepare('SELECT * FROM userinfo WHERE id=?'); if ($stmt == false) { var_dump($db->errno, $db->error); } else { $ret2 = $stmt->execute(array(10)); var_dump($ret2); } }); ~~~