多次从另一个PHP脚本调用php脚本

时间:2014-11-13 11:12:12

标签: php

我有这个代码用于从数据库中读取一些数据,并调用另一个php脚本(另一台服务器上的hostes)来发送检索到的数据。这是我的代码:

while ($rs = mysql_fetch_array($quary_result)) {
    $fields = array(
        'field1' => $rs['field1']
    );

    $postvars = http_build_query($fields);

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);

    $result = curl_exec($ch);
    curl_close($ch);
}

但此代码只执行一次。我的查询大约有200个结果,而只执行了一次。怎么了?

1 个答案:

答案 0 :(得分:1)

您的印象是它只执行一次......

您的代码说明了这一点:

while $rs = mysql_fetch_array($query_result) is true { do something }

然后每次进入while循环时都会覆盖$result var。尝试将$ result var转换为结果数组,然后打印数组以查看是否包含所有值:

$result[] = curl_exec($ch);
echo '<pre>';
   print_r($result);
echo '</pre>';

此外,您可以使用foreach()之类的:

$result = mysql_fetch_array($quary_result);
foreach ($result as $res) { do something }

并停止使用MYSQL - 这已被弃用。使用PDO就像在本教程中一样:PDO Tutorial

相关问题