Mr Chris Posted May 4, 2009 Share Posted May 4, 2009 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 More sharing options...
ohdang888 Posted May 4, 2009 Share Posted May 4, 2009 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/156839-solved-help-with-my-update-script/#findComment-826145 Share on other sites More sharing options...
Mr Chris Posted May 4, 2009 Author Share Posted May 4, 2009 Thanks v much! Link to comment https://forums.phpfreaks.com/topic/156839-solved-help-with-my-update-script/#findComment-826161 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.