-1

PHP配列に問題があります。$variable 配列にパスが書き込まれた get_headers() リクエストを送信しようとしています。MySQL データベースに書き込みたいと答えて、すでに数時間を費やしましたが、その方法がわかりませんでした。結果は 1 つしか返されませんが、配列からのエコー結果の場合は、たとえば 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
4

1 に答える 1

0

MySQLまず最初に、廃止されたものを使用しないでください。

使用MySQLi(i は改善用) http://www.php.net/manual/en/book.mysqli.php

foreach変数のonを使用しているarrayため、変数ごとINSERTにデータベースに入力されvariablesますarray

とは何$smthingですか?

arrayには 3 つの値しか含まれていませんが、30 を超えていると言っていINSERTSます。foreach}headers

ループの外側でエコーするforeachと、配列の最後の値のみが表示されます。

これも役立つかもしれませんhttps://stackoverflow.com/a/12782327/3754261

これを試して:

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
    }

}

条件文なしでこれを試す価値もあります

あなたの結果は?

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


}
于 2014-07-06T08:46:01.547 に答える