Miss-Ruth Posted December 13, 2010 Share Posted December 13, 2010 I need this script to go through each row of the mysql db and update the cat_num colum (for each record) with the new value. But this doen't seem to work. $marks = '25'; // this value is ever changing. For this example I'll put as 25 $query=mysql_query("SELECT * FROM table1"); $rownum=mysql_num_rows($query); for($i=0;$i<$rownum;$i++) { $query=mysql_query("SELECT * FROM table1 LIMIT $i,1"); while($select=mysql_fetch_array($query)) { $update=mysql_query("UPDATE table2 SET cat_num='$select[cat_num]'+$marks"); } } Quote Link to comment https://forums.phpfreaks.com/topic/221559-fori0i/ Share on other sites More sharing options...
priti Posted December 13, 2010 Share Posted December 13, 2010 $query=mysql_query("SELECT * FROM table1"); $rownum=mysql_num_rows($query); // you are fetching all records from table1 for($i=0;$i<$rownum;$i++) { $query=mysql_query("SELECT * FROM table1 LIMIT $i,1"); //why this is required here? you already fetched records from table1 while($select=mysql_fetch_array($query)) { $update=mysql_query("UPDATE table2 SET cat_num='$select[cat_num]'+$marks"); // print the update query } } Quote Link to comment https://forums.phpfreaks.com/topic/221559-fori0i/#findComment-1146909 Share on other sites More sharing options...
litebearer Posted December 13, 2010 Share Posted December 13, 2010 Close... This only gets the number of records that meet the query $rownum=mysql_num_rows($query); BTW your update (because you have not limited it ie WHERE something = something ) will result is ALL records in table2 being set to the same value Quote Link to comment https://forums.phpfreaks.com/topic/221559-fori0i/#findComment-1146932 Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 $marks = '25'; // this value is ever changing. For this example I'll put as 25 $query='SELECT * FROM table1'; $result=mysql_query($query); while($row=mysql_fetch_array($result)){ mysql_query('UPDATE table2 SET cat_num='.$row[cat_num]+$marks); } You may want to put the query string inside the actual mysql_query function, but don't be tempted to put the mysql_query inside the mysql_fetch_array while loop. It will then loop get a new mysql_query function each time, infinite, unless there is some remove query. I don't see why you need 25..., do you need to do it max 25 times? Because if so, just put a check at the end after the update query: $marks--; if($marks==0){ break; } $rownum=mysql_num_rows($query); I see why you want to use the number of rows, but I don't see why you actually need it... Quote Link to comment https://forums.phpfreaks.com/topic/221559-fori0i/#findComment-1146975 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.