foreach ($movies->channel->item as $item) {
echo $item->title.'<br />';
echo $item->link.'<br />';
echo $item->medium_image_url.'<br />';
echo $item->description.'<br />';
echo $item->city;
}
나는 그들에게 공감 한 코드가 각 반복에서 변수를 재 할당하지만 아무 것도 수행하지 않습니다. 이 값은 매번 덮어 쓰여 지므로 끝에있는 마지막 반복의 값만 유지합니다.
편집
쉽게는이 같은 수행하여 데이터베이스에 행을 삽입하기 위해 수정할 수있는
: 당신은 하나 개의 쿼리로 모두 바꿀 수 데이터베이스 트래픽을 최소화하기 위해, 또는
foreach ($movies->channel->item as $item) {
echo $item->title.'<br />';
echo $item->link.'<br />';
echo $item->medium_image_url.'<br />';
echo $item->description.'<br />';
echo $item->city.'<br />';
// Build the query - change the names of your table and columns as appropriate
$query = "INSERT INTO `tablename`
(`title`,`link`,`medium_image_url`,`description`,`city`)
VALUES
('$item->title','$item->link','$item->medium_image_url','$item->description','$item->city')";
// Do the query - do NOT show the output of mysql_error() in a production environment!
if (!mysql_query($query)) echo 'Oh no! Something went wrong with the query: '.mysql_error();
}
을 :
$rows = array();
foreach ($movies->channel->item as $item) {
echo $item->title.'<br />';
echo $item->link.'<br />';
echo $item->medium_image_url.'<br />';
echo $item->description.'<br />';
echo $item->city.'<br />';
// Add this entry to $rows
$rows[] = "('$item->title','$item->link','$item->medium_image_url','$item->description','$item->city')";
}
// Build the query - change the names of your table and columns as appropriate
$query = "INSERT INTO `tablename`
(`title`,`link`,`medium_image_url`,`description`,`city`)
VALUES ".implode(', ',$rows);
// Do the query - do NOT show the output of mysql_error() in a production environment!
if (!mysql_query($query)) echo 'Oh no! Something went wrong with the query: '.mysql_error();
'$ city' 라인은 편집이 필요합니다 ('-> item [$ i]'를 제거하십시오.) 잠재적으로'$ num' 변수는 더 이상 필요하지 않습니다. – Benjie
흠 .. 당신 말이 맞아 .. –
도와 주셔서 고마워요 – omnath