Php 将ForEach循环写入数据库
这是个愚蠢的问题,但我似乎找不到答案 我有一个foreach循环,用于查找特定链接上的所有图像。我想对所有这些图像进行编码,然后将它们写入数据库。如果我将sql查询放在foreach循环内,则会为每个图像创建一个新行,如果我将sql查询放在foreach循环外,则只会写入第一个图像。如果我尝试分别写入图像和其余数据,insert会将$newimage视为null并覆盖这些值。如有任何建议,将不胜感激Php 将ForEach循环写入数据库,php,mysql,Php,Mysql,这是个愚蠢的问题,但我似乎找不到答案 我有一个foreach循环,用于查找特定链接上的所有图像。我想对所有这些图像进行编码,然后将它们写入数据库。如果我将sql查询放在foreach循环内,则会为每个图像创建一个新行,如果我将sql查询放在foreach循环外,则只会写入第一个图像。如果我尝试分别写入图像和其余数据,insert会将$newimage视为null并覆盖这些值。如有任何建议,将不胜感激 foreach($html->find('img') as $images) { $new
foreach($html->find('img') as $images) {
$newimage = json_encode($images->src);
echo $newimage;
}
$sql="INSERT INTO data (headline, images, email, date, category, area, number, crawled, lastcrawled, description)
VALUES ('$headline', '$newimage', '$email','$date','$category','$area','$number', '$crawled', '$dt', '$description')";
希望这对你有帮助
$newimages = array();
foreach($html->find('img') as $images) {
$newimages[] = $images->src;
}
$newimage = json_encode($newimages);
同样的sql-
$sql="INSERT INTO data (headline, images, email, date, category, area, number,
crawled, lastcrawled, description) VALUES ('$headline', '$newimage',
$email','$date','$category','$area','$number', '$crawled', '$dt', '$description')";
根据您的评论,这将为您提供所需信息:
// create an array to hold the image sources
$store_images = array();
foreach($html->find('img') as $images) {
// add the image sources to the array
$store_images[] = $images->src;
}
// encode the entire array of images
$json_store_images = json_encode($store_images);
// store the encoded images along with the other data
$sql="INSERT INTO data (headline, images, email, date, category, area, number, crawled, lastcrawled, description)
VALUES ('$headline', '$json_store_images', '$email','$date','$category','$area','$number', '$crawled', '$dt', '$description')";
你说你不想要的,但你从不说你想要的——如果他们不都在自己的行中,你想让他们都在一列中吗?多列?嗨,我想把它们都放在一列里。图像。以下是我在专栏中想要的内容:“http:\/\/www.expariates.com\/img\/x24206822.1.jpg.pagespeed.ic.w2EvKt56b8.jpg”“http:\/\/www.expariates.com\/img\/x24206822.2.jpg.pagespeed.ic.w2EvKt56b8.jpg”“http:\/\/www.expariates.com\/img\/x24206822.3.jpg.pagespeed.ic.jmiladlcu.jpg”“http:\/\/www.expatilates.com\/img\/x24206822.4.jpg.pagespeed.ic.NLTh4PoNn0.jpg”“http:\/\/www.expatilates.com\/img\/x24206822.5.jpg.pagespeed.ic.2aT-l3yOqk.jpg”