Php file_get_contents()只给出for循环中的最后一个值
我试图获取url的内容并保存在mysql中,但我只能获取最后一个值Php file_get_contents()只给出for循环中的最后一个值,php,Php,我试图获取url的内容并保存在mysql中,但我只能获取最后一个值 for($i = 1; $i < 5; $i++) { $url = "http://something.php/$i"; $content = file_get_contents($url); echo '<pre>'; print_r($content); echo '</pre>'; } ($i=1;$i
for($i = 1; $i < 5; $i++) {
$url = "http://something.php/$i";
$content = file_get_contents($url);
echo '<pre>';
print_r($content);
echo '</pre>';
}
($i=1;$i<5;$i++)的{
$url=”http://something.php/$i”;
$content=file\u get\u contents($url);
回声';
打印(内容);
回声';
}
像这样附加$content
$contents = array();
for($i = 1; $i < 5; $i++) {
$url = "http://something.php/$i";
array_push($contents, file_get_contents($url));
echo '<pre>';
print_r($contents[$i-1]);
echo '</pre>';
}
您正在覆盖循环每次迭代中的内容。根据您希望在mySQL中保存内容的方式,您需要执行以下两项操作之一:
<?php
// CONNECT TO THE DATABASE
$DB_NAME = 'DATABASE_NAME';
$DB_HOST = 'DATABASE_HOST';
$DB_USER = 'DATABASE_USER';
$DB_PASS = 'DATABASE_PASSWORD';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
for($i = 1; $i < 5; $i++) {
$url = "http://something.php/$i";
$content = file_get_contents($url);
$query = "INSERT INTO `urls` (`url`,`html`) VALUES ('$url', '$content');";
if ( $mysqli->query($query) ) {
echo "A new entry has been added with the `id` of {$mysqli->insert_id}.";
} else {
echo "There was a problem:<br />$query<br />{$mysqli->error}";
}
}
/* close our connection */
$mysqli->close();
?>
$contents=array();
对于($i=1;$i<5;$i++){
$url=”http://something.php/$i”;
数组_push($contents,file_get_contents($url));
回声';
打印($contents[$i-1]);
回声';
}
现在你有了可以操作的东西。这可能对你有用:
为了解释,对于那些没有看到它的人,“=”之前的“.”会附加到给定的变量,而仅仅使用“=”会重写值谢谢Ganesh ji爵士,但它对我不起作用。仍然只得到最后一个值hi@Floris,我仍然没有得到who url值。我刚刚意识到你的循环是从1到5-
array\u push
通过填充元素0
开始。对代码进行了相应的调整。现在好点了吗?上述操作应产生一个数组$contents[]
(注意,末尾有一个s
)$contents[0]
应包含对第一个文件\u get\u contents()
调用的响应,等等。打印的内容是什么($contents)
(无索引)给你的?可能是对http://something.php/1
是否实际为空?移除循环时会发生什么-只需设置$i=1代码>。那么你得到第一个值了吗?因为我的代码和@Tuga's在每次迭代中都应该做一些有用的事情……嗨,Tuga,@Tuga,我试着这么做。只获取loopAlter数据库的最后一个值,并将id
字段设置为AUTO_INCREMENT
,这似乎是一个非常明智的解决方案(它扩展了我的建议1)。应该可以。@user3226210我已经更新了我的答案,现在它包含了创建数据库以处理上述代码所需的代码,请查看。
<?php
// CONNECT TO THE DATABASE
$DB_NAME = 'DATABASE_NAME';
$DB_HOST = 'DATABASE_HOST';
$DB_USER = 'DATABASE_USER';
$DB_PASS = 'DATABASE_PASSWORD';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
for($i = 1; $i < 5; $i++) {
$url = "http://something.php/$i";
$content = file_get_contents($url);
$query = "INSERT INTO `urls` (`url`,`html`) VALUES ('$url', '$content');";
if ( $mysqli->query($query) ) {
echo "A new entry has been added with the `id` of {$mysqli->insert_id}.";
} else {
echo "There was a problem:<br />$query<br />{$mysqli->error}";
}
}
/* close our connection */
$mysqli->close();
?>
CREATE TABLE `urls` (
`id` int NOT NULL AUTO_INCREMENT ,
`url` varchar(80) NOT NULL ,
`html` varchar(50000) NOT NULL
)
;