栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PHP / MySQL-同时进行多个查询

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP / MySQL-同时进行多个查询

使用curl_multi_open可以做到这一点

将您的脚本分成两部分,您可以使一个php文件(例如email_out.php)具有数据库名称(或一些用于查找数据库名称的变量,该开关将位于for循环中或位于email_out.php中)
,然后基于该脚本进行大量电子邮件发送。

第二部分使用curl_multi_open多次打开email_out.php脚本,有效地创建了到不同数据库的多个单独连接,这些脚本都可以在不同时间解析,因为它们都是并行运行的。本质上,您的循环现在是使用不同的参数多次将脚本添加到curl_multi_open,然后异步执行所有脚本。

class Fork{    private $_handles = array();    private $_mh      = array();    function __construct()    {        $this->_mh = curl_multi_init();    }    function add($url)    {        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_HEADER, 0);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        curl_setopt($ch, CURLOPT_TIMEOUT, 30);        curl_multi_add_handle($this->_mh, $ch);        $this->_handles[] = $ch;        return $this;    }    function run()    {        $running=null;        do { curl_multi_exec($this->_mh, $running); usleep (250000);        } while ($running > 0);        for($i=0; $i < count($this->_handles); $i++) { $out = curl_multi_getcontent($this->_handles[$i]); $data[$i] = json_depre($out); curl_multi_remove_handle($this->_mh, $this->_handles[$i]);        }        curl_multi_close($this->_mh);        return $data;    }}

(来自http://gonzalo123.com/2010/10/11/speed-up-php-scripts-with-asynchronous-
database-queries/

因此,您的循环将如下所示:

$fork = new Fork;for ($i = 0; $i < 24; $i++) {    $fork->add("email_out.php?t=" . $i);}$fork->run();


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/670269.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号