Jump to content

[SOLVED] Help with my update script?


Mr Chris

Recommended Posts

Hello,

 

I have two sets of tables in mysql database held_stories and new_stories.

 

Now, what I want to do is get all the data from held_stories  and update new_stories with that information where the story_id's for each table match.

 

Can anyone help me please, I think i'm nearly there?

 


<?php 

$n=1; 

// Start query one and get all the details from held stories

$query_one = "SELECT story_id, headline from held_stories"; 
$result = mysql_query($query_one)  OR die(mysql_error()); 
while ($row = mysql_fetch_array($result)) { 
   $story_id = $row['story_id'];
   $headline = $row['headline']; 

//Start the second query to update the new stories with the held stories details

$query_two = "UPDATE new_stories set headline = '$headline' WHERE story_id = $story_id"; 
$result = mysql_query($query_two)  OR die(mysql_error()); 

$n++; }

$number_of_results = mysql_num_rows($result); 

If (mysql_num_rows($result) > 0) { 
  echo "We have updated <b>".$number_of_results."</b> stories in the new_stories database"; 
} 
else { 
  $row = mysql_fetch_array( $result ); 
  echo "No stories have been updated"; 
} 

?>

Link to comment
https://forums.phpfreaks.com/topic/156839-solved-help-with-my-update-script/
Share on other sites

a few minor revisions to you code:

 

this should do it..

 

btw,  $n looks pointless here, consider deleting it.

<?php 

$n=1; 

// Start query one and get all the details from held stories

$query_one = "SELECT story_id, headline from held_stories"; 
$result = mysql_query($query_one)  OR die(mysql_error()); 

while ($row = mysql_fetch_array($result)) { 
   $story_id = $row['story_id'];
   $headline = $row['headline']; 

//Start the second query to update the new stories with the held stories details

$query_two = "UPDATE new_stories SET headline = '$headline' WHERE story_id = '$story_id' "; 
mysql_query($query_two)  OR die(mysql_error()); 

$n++; }

$number_of_results = mysql_num_rows($result); 

If (mysql_num_rows($result) > 0) { 
  echo "We have updated <b>".$number_of_results."</b> stories in the new_stories database"; 
} 
else { 
  echo "No stories have been updated"; 
} 

?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.