sinus Posted August 28, 2007 Share Posted August 28, 2007 I have code that lists a number of people from a data and stores it array // select people from first table who are part of group 1 $query= "SELECT personID FROM people WHERE groupID = 1"; $result = mysql_query($query); $numrows = mysql_num_rows($result); while ($row = mysql_fetch_array($result) { $personid = $row["personID"]; } The problem is I do not know how many records it will return. I then need to enter all these people into another table. // add people to second table who are part of group 1 $people = 0; while ($people != $numrows) { $query2 = "INSERT INTO people2 (peopleID, groupID) VALUES ('$personid', '1')"; $queryaddperson = mysql_query($query2) or die(mysql_error()); $people++; } Rather than add every single person who is part of group 1 it just keeps adding the first person over and over again to the people2 table. Any help with this would be great. Thanks Sinus Quote Link to comment https://forums.phpfreaks.com/topic/67146-solved-retriving-data-from-a-database/ Share on other sites More sharing options...
Fadion Posted August 28, 2007 Share Posted August 28, 2007 U dont know how many records it will return? U have those in $numrows!! Anyway, your problem is that the update snippet must either be inserted in the while($row = mysql_fetch_array){ } or make $personid an array: $personid = array(); while ($row = mysql_fetch_array($result) { array_push($personid, $row['personID']; } foreach($personid as $value){ $query2 = "INSERT INTO people2 (peopleID, groupID) VALUES ('$value', '1')"; $queryaddperson = mysql_query($query2) or die(mysql_error()); } U got it know? Quote Link to comment https://forums.phpfreaks.com/topic/67146-solved-retriving-data-from-a-database/#findComment-336744 Share on other sites More sharing options...
madspof Posted August 28, 2007 Share Posted August 28, 2007 i dont know if this helps but i think you need to find a way off putting the information taken from teh db into an array and then inserting it back the other table but im not sure Quote Link to comment https://forums.phpfreaks.com/topic/67146-solved-retriving-data-from-a-database/#findComment-336747 Share on other sites More sharing options...
sinus Posted August 29, 2007 Author Share Posted August 29, 2007 Hello Thankyou for the replies. So this // select people from first table who are part of group 1 $query= "SELECT personID FROM people WHERE groupID = 1"; $result = mysql_query($query); $numrows = mysql_num_rows($result); while ($row = mysql_fetch_array($result) { $personid = $row["personID"]; // add people to second table who are part of group 1 $query2 = "INSERT INTO people2 (peopleID, groupID) VALUES ('$personid', '1')"; $queryaddperson = mysql_query($query2) or die(mysql_error()); } and this would do the exact same thing // select people from first table who are part of group 1 $query= "SELECT personID FROM people WHERE groupID = 1"; $result = mysql_query($query); $personid = array(); while ($row = mysql_fetch_array($result) { array_push($personid, $row['personID']; } // add people to second table who are part of group 1 foreach($personid as $value){ $query2 = "INSERT INTO people2 (peopleID, groupID) VALUES ('$value', '1')"; $queryaddperson = mysql_query($query2) or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/67146-solved-retriving-data-from-a-database/#findComment-336901 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.