如何从数组获取数据到字符串php

时间:2014-07-06 06:41:46

标签: php mysql arrays variables get-headers

有php数组的问题。  我试图发送带有写入$ variable数组的路径的get_headers()请求,回答我想写入MySQL数据库,花了几个小时,但没有找到如何做到这一点。它只返回1个结果,但如果来自数组的echo结果 - 可以看到例如3个结果。拜托,帮帮我吧。)

    foreach($array_variable as $variable) {
$url = "http://".$site.$variable;
$file_headers = @get_headers($url);
if($file_headers[0] == 'HTTP/1.1 200 OK') {

    $test = $url;
     echo $test;  //here it works fine, I can see all available results
      $sql = mysql_query("INSERT INTO table (col_1, col_2) VALUES ($smthing, $test)"); //here problem is that result duplicates many-many times

}
 echo $test; //but here I have problems, can see only 1 result (last one)
 $sql = mysql_query("INSERT INTO table (col_1, col_2) VALUES ($smthing, $test)"); //here problem is, only 1 result goes to our database

1 个答案:

答案 0 :(得分:0)

首先,不要使用MySQL弃用。

使用MySQLi(我需要改进)http://www.php.net/manual/en/book.mysqli.php

您在foreach变量上使用array,因此对于每个变量INSERT进入数据库,variables中有array$smthing

什么是array

你说INSERTS只包含3个值,但是你可以超过30 foreach可能是你没有关闭}这需要headers或者你是从foreach获得多个回复。

如果你在foreach($array_variable as $variable) { $url = "http://".$site.$variable; $file_headers = @get_headers($url); if($file_headers[0] == 'HTTP/1.1 200 OK') { $test = $url; echo $test; //here it works fine, I can see all available results $sql = mysql_query("INSERT INTO table (col_1, col_2) VALUES ($smthing, $test)"); //here problem is that result duplicates many-many times } } 循环之外回显,你将只看到数组的最后一个值。

这也可能有助https://stackoverflow.com/a/12782327/3754261

试试这个:

foreach($array_variable as $variable) {
    $url = "http://".$site.$variable;
    $file_headers = @get_headers($url);

    $test = $url;
    echo $test;  //here it works fine, I can see all available results
    $sql = mysql_query("INSERT INTO table (col_1, col_2) VALUES ($smthing, $test)");     //here problem is that result duplicates many-many times


}

如果没有条件声明

,也值得尝试

您的结果是什么?

{{1}}