-1

Have problem with php array. I'm trying to send get_headers() request with paths written into $variable array, answer I want to write into MySQL database, spent few hours already, but didn't find out how to do that. It returns only 1 result, but if echo results from array - can see for example 3 results. Please, help me somebody :)

    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
  • After `foreach` is over `$test` contains the last assigned value. What did you expect? – u_mulder Jul 06 '14 at 06:46
  • Thanks for your response. I'm new to php, so some questions could be a bit stupid, sorry :) As I wrote previously - I want get valid data from array and write to my database (mysql), is it possible ? – cybersecuriosity.com Jul 06 '14 at 06:49
  • `$test` is only initilised if the response is 200. It may be that in some early iterations it does not meet that condition, therefore `$test` is not set, and nothing is written to the database. – Alex Jul 06 '14 at 07:28
  • Ah, not. I have of course another rule for such situations out of 'foreach' that writes warning to database if response 200 is not met. I want to know, how I can correctly write those data from $test variable to mysql database, because when I'm trying to serialize() it or json_encode - it works, but still writes over 30 (half of them are duplicates) fields to database. – cybersecuriosity.com Jul 06 '14 at 07:51

1 Answers1

0

First things first, don't use MySQL its deprecated.

Use MySQLi (i is for improved) http://www.php.net/manual/en/book.mysqli.php

Your using a foreach on an array of variables so for each variable it will INSERT into the database, how many variables are in the array?

What is the $smthing?

You say the array only contains 3 values yet you get over 30 INSERTS it could be that your not closing your foreach this is needed } or that you are getting multiple replys from headers.

If you echo outside the foreach loop you will only see the last value of the array.

This might help also https://stackoverflow.com/a/12782327/3754261

Try this:

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
    }

}

It would also be worth trying this without the conditional statement

What are your results?

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


}
Community
  • 1
  • 1
CodeX
  • 313
  • 1
  • 14