我需要从网址抓取图片,然后将网址更改为其他网址。这是我的代码。但它没有像我预期的那样工作。它只会改变一个图像。其他人不胜。
这是我的代码,
<?php
include_once('db-conn.php');
$query = "SELECT my_image_url AS image FROM my_image";
$result = mysqli_query($mysqli, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($mysqli), E_USER_ERROR);
if($result) {
while($row = mysqli_fetch_assoc($result)) {
$img_url = $row['image'];
$content = file_get_contents($img_url);
$img_name = basename($img_url);
file_put_contents($img_name, $content);
$query = "UPDATE `my_image` SET `my_image_url` = 'http://localhost/img/".$img_name."'";
$result2 = mysqli_query($mysqli, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($mysqli), E_USER_ERROR);
}
}
?>
答案 0 :(得分:1)
将查询更改为下面的查询,以便更新抓取值的列
$query = "UPDATE `my_image`
SET `my_image_url` = 'http://localhost/img/".$img_name."'
WHERE `my_image_url` = {$img_url}";
答案 1 :(得分:0)
您应该为update
查询添加条件
$query = "UPDATE `my_image` SET `my_image_url` = 'http://localhost/img/".$img_name."' where (YOUR CONDITION HERE)";