coolbeansdude51 Posted May 21, 2009 Share Posted May 21, 2009 Hello all, Here's whats up. I have a form which has a list of groups. The list of groups correspond with a groupid field in the usersgroups table. Each user can have multiple groups. For each group that the user is in there is a record in the table. The $groups variable is the $_POST['group'] from the form. In the form they can select multiple groups. I have [] in the form so no worries there. I need to select different users in the groups and then add them to an array ($mailer[]) to process through a mailer function that we have. I don't get any mysql errors or anything. Everything checks out with that. Yet I don't get the expected results or desired results. Can you all check my logic and help me out. When I echo out $row["userid"] 14 comes out ... I don't have a user with 14 as the id or even in my database at all. I don't know how that happened. Thanks in advance for your wisdom! foreach($groups as $g){ $query = "SELECT * FROM usersgroups WHERE groupid = '$g'"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result) or die(mysql_error())) { echo $row["userid"]; $query1 = "SELECT * FROM people WHERE id = '".$row["userid"]."'"; $result1 = mysql_query($query1) or die(mysql_error()); $row1 = mysql_fetch_array($result1) or die(mysql_error()); $mailer[] = $row1['phone'].$row1['provider']; }; } Quote Link to comment https://forums.phpfreaks.com/topic/159018-solved-looping-through-form-data-with-multiple-mysql_selects/ Share on other sites More sharing options...
michaellunsford Posted May 21, 2009 Share Posted May 21, 2009 mysql_fetch_array will return $row[0],$row[1],$row[2], etc. If you want $row['userid'] you'll need to call mysql_fetch_assoc() instead. It's the same with $row1['phone'] and ['provider']. Quote Link to comment https://forums.phpfreaks.com/topic/159018-solved-looping-through-form-data-with-multiple-mysql_selects/#findComment-838627 Share on other sites More sharing options...
curtis_b Posted May 21, 2009 Share Posted May 21, 2009 this won't solve your problem, but try making it one query: SELECT * FROM people, usergroups WHERE usersgroups.userid = people.id AND groupid = '$g' Quote Link to comment https://forums.phpfreaks.com/topic/159018-solved-looping-through-form-data-with-multiple-mysql_selects/#findComment-838628 Share on other sites More sharing options...
xtopolis Posted May 21, 2009 Share Posted May 21, 2009 @michaellunsford fyi: http://us2.php.net/mysql_fetch_array "By using MYSQL_BOTH (default), you'll get an array with both associative and number indices." Quote Link to comment https://forums.phpfreaks.com/topic/159018-solved-looping-through-form-data-with-multiple-mysql_selects/#findComment-838631 Share on other sites More sharing options...
michaellunsford Posted May 21, 2009 Share Posted May 21, 2009 @xtopolis yeah, you're right. I must have confused it with mysql_fetch_row. Where's the unpost button? Quote Link to comment https://forums.phpfreaks.com/topic/159018-solved-looping-through-form-data-with-multiple-mysql_selects/#findComment-838637 Share on other sites More sharing options...
coolbeansdude51 Posted May 21, 2009 Author Share Posted May 21, 2009 That worked. Thanks for the help! The final code looks like this. Any suggestions? foreach($groups as $g){ $query = "SELECT * FROM people, usersgroups WHERE usersgroups.userid = people.id AND groupid = '$g'"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_assoc($result) or die(mysql_error())) { echo $row['phone']; }; } }; [/] Quote Link to comment https://forums.phpfreaks.com/topic/159018-solved-looping-through-form-data-with-multiple-mysql_selects/#findComment-838695 Share on other sites More sharing options...
coolbeansdude51 Posted May 21, 2009 Author Share Posted May 21, 2009 OK. I tried it trying to not just echo out the data but add it to an array and that didn't work. Any suggestions? Here's the code: foreach($groups as $g){ $query = "SELECT * FROM people, usersgroups WHERE usersgroups.userid = people.id AND groupid = '$g'"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_assoc($result) or die(mysql_error())) { echo $row['phone']; $test[] = $row['phone']; }; }; print_r($test); It echo's out the phone numbers but never adds them to the array ... any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/159018-solved-looping-through-form-data-with-multiple-mysql_selects/#findComment-838703 Share on other sites More sharing options...
michaellunsford Posted May 21, 2009 Share Posted May 21, 2009 $test[$i++]=$row['phone']; Quote Link to comment https://forums.phpfreaks.com/topic/159018-solved-looping-through-form-data-with-multiple-mysql_selects/#findComment-838906 Share on other sites More sharing options...
coolbeansdude51 Posted May 21, 2009 Author Share Posted May 21, 2009 Thanks for the reply michaellunsford! That didn't work ... Here is the entire piece of code I am working with. I hope this helps. if($error == 'yes') { $_SESSION['mes']['type'] = 'error'; unset($error); header('Location: send_message.php'); exit; } else { require('../lib/functions.php'); require_once('../db.php'); unset($error); $content = sanitize($_POST['content']); $groups = $_POST['group']; foreach($groups as $g){ $query = "SELECT * FROM people, usersgroups WHERE usersgroups.userid = people.id AND groupid = '$g'"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_assoc($result) or die(mysql_error())) { echo $row['phone']; $test[$i++] = $row['phone']; }; echo 'test'; var_dump($test); }; }; It does echo out the $row['phone'] fine. Right now ... It doesn't even echo out the test or do a var_dump of $test. What am I missing? It seems like I am just not seeing something. Probably because I have yet to sleep ... go figure. Any suggestions anyone? Thanks for the help so far! Quote Link to comment https://forums.phpfreaks.com/topic/159018-solved-looping-through-form-data-with-multiple-mysql_selects/#findComment-839058 Share on other sites More sharing options...
coolbeansdude51 Posted May 21, 2009 Author Share Posted May 21, 2009 Its something to do with the while statement. If I comment it out it works fine. Humm. Idea's? Quote Link to comment https://forums.phpfreaks.com/topic/159018-solved-looping-through-form-data-with-multiple-mysql_selects/#findComment-839089 Share on other sites More sharing options...
coolbeansdude51 Posted May 21, 2009 Author Share Posted May 21, 2009 After a ton of trial and error I figured it out. The or die(mysql_error()) was the problem. Cut that out and it works like a champ. Here's the code that works. if($error == 'yes') { $_SESSION['mes']['type'] = 'error'; unset($error); header('Location: send_message.php'); exit; } else { require('../lib/functions.php'); require_once('../db.php'); $content = sanitize($_POST['content']); $groups = $_POST['group']; foreach($groups as $g){ $query = "SELECT * FROM people, usersgroups WHERE usersgroups.userid = people.id AND groupid = '$g'"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $test[$i++] = $row['phone']; }; }; }; var_dump($test); Any ideas why that would make it crap out? Quote Link to comment https://forums.phpfreaks.com/topic/159018-solved-looping-through-form-data-with-multiple-mysql_selects/#findComment-839107 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.