多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
~~~ <?php $begin = time(); //同步请求 function multi_sync(){ $host = '192.168.2.87'; $user = 'census'; $password = 'census'; $database = 'census'; $mysqli = new mysqli($host, $user, $password, $database); $query = "SELECT sleep(1);" ; $query .= "SELECT sleep(1);" ; if ( $mysqli -> multi_query ( $query )) { do { echo "multi_sync_query \n"; if ( $result = $mysqli -> store_result ()) { $ret = $result->fetch_array(MYSQLI_ASSOC); var_dump($ret); } // if ( $mysqli -> more_results ()) { // printf ( "-----------------\n" ); // } } while ( $mysqli -> more_results () && $mysqli -> next_result ()); } } multi_sync(); //同步请求 /* function sync(){ $host = '192.168.2.87'; $user = 'census'; $password = 'census'; $database = 'census'; $db = new mysqli($host, $user, $password, $database); echo "sync_query 1\n"; $statement1 = $db->query('select sleep(1)'); $ret1 = $statement1->fetch_array(MYSQLI_ASSOC); echo "sync_query 2\n"; $statement2 = $db->query('select sleep(1)'); $ret2 = $statement2->fetch_array(MYSQLI_ASSOC); var_dump($ret1); var_dump($ret2); } sync(); */ //携程异步---php携程,mysqlnd异步 /* function f1(){ $db = new db(); $obj = $db->async_query('select sleep(1)'); echo "f1 async_query \n"; yield $obj; $row = $db->fetch(); echo "f1 fetch\n"; yield $row; } function f2(){ $db = new db(); $obj = $db->async_query('select sleep(1)'); echo "f2 async_query\n"; yield $obj; $row = $db->fetch(); echo "f2 fetch\n"; yield $row; } $gen1 = f1(); $gen2 = f2(); $gen1->current(); $gen2->current(); $gen1->next(); $gen2->next(); $ret1 = $gen1->current(); $ret2 = $gen2->current(); var_dump($ret1); var_dump($ret2); */ echo time() - $begin; class db{ static $links; private $obj; function getConn(){ $host = '192.168.2.87'; $user = 'census'; $password = 'census'; $database = 'census'; $this->obj = new mysqli($host, $user, $password, $database); self::$links[spl_object_hash($this->obj)] = $this->obj; return self::$links[spl_object_hash($this->obj)]; } function async_query($sql){ $link = $this->getConn(); $link->query($sql, MYSQLI_ASYNC); return $link; } function fetch(){ //for($i = 1; $i <= 5; $i++){ $read = $errors = $reject = self::$links; $re = mysqli_poll($read, $errors, $reject, 1); foreach($read as $obj){ if($this->obj === $obj){ $sql_result = $obj->reap_async_query(); $sql_result_array = $sql_result->fetch_array(MYSQLI_ASSOC);//只有一行 $sql_result->free(); return $sql_result_array; } } //} } } ~~~ 异步之后只要1秒,同步以及同步多请求,都是2秒