有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
答案 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}}