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
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
}