【发布时间】:2014-07-06 06:41:45
【问题描述】:
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
【问题讨论】:
-
在
foreach结束后$test包含最后分配的值。你期待什么? -
感谢您的回复。我是 php 新手,所以有些问题可能有点愚蠢,抱歉 :) 正如我之前写的 - 我想从数组中获取有效数据并写入我的数据库(mysql),这可能吗?
-
$test仅在响应为 200 时才被初始化。可能是在某些早期迭代中它不满足该条件,因此未设置$test,并且没有任何内容写入数据库. -
啊,不是。对于这种情况,我当然有另一条规则,如果不满足响应 200,则将警告写入数据库。我想知道,如何正确地将这些数据从 $test 变量写入 mysql 数据库,因为当我尝试对其进行序列化()或 json_encode 时 - 它可以工作,但仍然写入 30 多个(其中一半是重复的)字段到数据库。
标签: php mysql arrays variables get-headers