Re: [PHP] Creating arrays using results from MySQL query
You have to call mysql_fetch_array for each record in your result set... $emp_login_wkgrp_id = array (); $emp_login_grp_id = array (); $emp_login_role_id = array (); $i = 0; while($employee_2 = mysql_fetch_array($result_2)){ $emp_login_wkgrp_id[$i] = $employee_2[wkgrp_id]; $emp_login_grp_id[$i] = $employee_2[grp_id]; $emp_login_role_id[$i] = $employee_2[role_id]; $i++; } mysql_fetch_array will return false when you run out of results, breaking the while loop. Check the manual for more info: http://www.php.net/manual/en/function.mysql-fetch-array.php mh. On Mon, 18 Mar 2002, Mullin, Reginald wrote: Hi Guys, I've been experiencing some problems when trying to build 3 arrays with the ID values of all of the groups a user belongs to. (I then want to register these arrays into the current session). The arrays only appear to be getting the first value (group ID) instead of all of the values the user belongs to. What am I doing wrong here? My code looks like this: File: login.php # if $employee_1, query db workgroups table to check if $emp_login_id belongs to any groups $sql_2 = SELECT * FROM workgroups WHERE emp_id='$emp_login_id'; $result_2 = @mysql_query($sql_2) or die (mysql_error()); $rows = mysql_num_rows($result_2); $employee_2 = mysql_fetch_array($result_2); # if match, set workgroups login variables in array, then register workgroups login variables in session if ($employee_2){ $emp_login_wkgrp_id = array (); $emp_login_grp_id = array (); $emp_login_role_id = array (); for ($i=0; $i$rows; $i++){ $emp_login_wkgrp_id[$i] = $employee_2[wkgrp_id]; $emp_login_grp_id[$i] = $employee_2[grp_id]; $emp_login_role_id[$i] = $employee_2[role_id]; } session_register('emp_login_wkgrp_id'); session_register('emp_login_grp_id'); session_register('emp_login_role_id'); } O From Now 'Till Then, \-Reginald Alex Mullin /\ 212-894-1690 ** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the postmaster at [EMAIL PROTECTED] www.sothebys.com ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Creating arrays using results from MySQL query
Mark, I'm still experiencing the same problem. Only one record is being added to the arrays. There are currently two records in the DB workgroups table matching the WHERE emp_id='$emp_login_id criteria. Here's the 2 records in the DB workgroups table: FIELDS Value#1 Value#2 wkgrp_id1 4 grp_id 111 222 emp_id 39 39 wkgrp_create_date 2002-03-16 23:45:43 -00-00 00:00:00 wkgrp_change_date 2002-03-16 23:45:43 -00-00 00:00:00 wkgrp_change_by webmaster webmaster wkgrp_create_by webmaster webmaster role_id 1 1 I'm only getting back Value#2. Here's the modified PHP code: File: login.php if ($employee_2){ $emp_login_wkgrp_id = array(); $emp_login_grp_id = array(); $emp_login_role_id = array(); $i = 0; while($employee_2 = mysql_fetch_array($result_2)){ $emp_login_wkgrp_id[$i] = $employee_2[wkgrp_id]; $emp_login_grp_id[$i] = $employee_2[grp_id]; $emp_login_role_id[$i] = $employee_2[role_id]; $i++; } session_register('emp_login_wkgrp_id'); session_register('emp_login_grp_id'); session_register('emp_login_role_id'); } O From Now 'Till Then, \-Reginald Alex Mullin /\ 212-894-1690 -Original Message- From: Mark Heintz PHP Mailing Lists [SMTP:[EMAIL PROTECTED]] Sent: Monday, March 18, 2002 1:26 PM To: Mullin, Reginald Cc: [EMAIL PROTECTED] Subject: Re: [PHP] Creating arrays using results from MySQL query You have to call mysql_fetch_array for each record in your result set... $emp_login_wkgrp_id = array (); $emp_login_grp_id = array (); $emp_login_role_id = array (); $i = 0; while($employee_2 = mysql_fetch_array($result_2)){ $emp_login_wkgrp_id[$i] = $employee_2[wkgrp_id]; $emp_login_grp_id[$i] = $employee_2[grp_id]; $emp_login_role_id[$i] = $employee_2[role_id]; $i++; } mysql_fetch_array will return false when you run out of results, breaking the while loop. Check the manual for more info: http://www.php.net/manual/en/function.mysql-fetch-array.php mh. On Mon, 18 Mar 2002, Mullin, Reginald wrote: Hi Guys, I've been experiencing some problems when trying to build 3 arrays with the ID values of all of the groups a user belongs to. (I then want to register these arrays into the current session). The arrays only appear to be getting the first value (group ID) instead of all of the values the user belongs to. What am I doing wrong here? My code looks like this: File: login.php # if $employee_1, query db workgroups table to check if $emp_login_id belongs to any groups $sql_2 = SELECT * FROM workgroups WHERE emp_id='$emp_login_id'; $result_2 = @mysql_query($sql_2) or die (mysql_error()); $rows = mysql_num_rows($result_2); $employee_2 = mysql_fetch_array($result_2); # if match, set workgroups login variables in array, then register workgroups login variables in session if ($employee_2){ $emp_login_wkgrp_id = array (); $emp_login_grp_id = array (); $emp_login_role_id = array (); for ($i=0; $i$rows; $i++){ $emp_login_wkgrp_id[$i] = $employee_2[wkgrp_id]; $emp_login_grp_id[$i] = $employee_2[grp_id]; $emp_login_role_id[$i] = $employee_2[role_id]; } session_register('emp_login_wkgrp_id'); session_register('emp_login_grp_id'); session_register('emp_login_role_id'); } O From Now 'Till Then, \-Reginald Alex Mullin /\ 212-894-1690 ** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the postmaster at [EMAIL PROTECTED] www.sothebys.com ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Creating arrays using results from MySQL query
Hi Guys, I've finally been able to figure this out. Turns out, I had to make another, different SQL query to the DB instead of re-using the prior SQL query statement. The *new* code looks like this: File: login.php # if $employee_1, query db workgroups table to check if $emp_login_id belongs to any groups $sql_2 = SELECT * FROM workgroups WHERE emp_id='$emp_login_id'; $result_2 = @mysql_query($sql_2) or die (mysql_error()); $employee_2 = mysql_fetch_array($result_2); # if match, set workgroups login variables in array, then register workgroups login variables in session if ($employee_2){ $emp_login_wkgrp_id = array(); $emp_login_grp_id = array(); $emp_login_role_id = array(); $i=0; $result_3 = @mysql_query($sql_2) or die (mysql_error()); while($employee_3 = mysql_fetch_array($result_3)){ $emp_login_wkgrp_id[$i] = $employee_3[wkgrp_id]; $emp_login_grp_id[$i] = $employee_3[grp_id]; $emp_login_role_id[$i] = $employee_3[role_id]; $i++; } session_register('emp_login_wkgrp_id'); session_register('emp_login_grp_id'); session_register('emp_login_role_id'); } Now when I print_r(array_values($emp_login_grp_id)); I get the following values: Array ( [0] = 111 [1] = 222 [2] = 333 ). Many thanks to everyone (Mark) for all of your help. O From Now 'Till Then, \-Reginald Alex Mullin /\ 212-894-1690 -Original Message- From: Mullin, Reginald Sent: Monday, March 18, 2002 2:45 PM To: '[EMAIL PROTECTED]' Subject: FW: [PHP] Creating arrays using results from MySQL query I've just added another record to the table. Now they're a total of 3 records matching the WHERE emp_id='$emp_login_id criteria. When I print_r(array_values($emp_login_grp_id)); I get the following values: Array ( [0] = 222 [1] = 333 ). For some reason, it seems to be skipping the first record. O From Now 'Till Then, \-Reginald Alex Mullin /\ 212-894-1690 -Original Message- From: Mullin, Reginald Sent: Monday, March 18, 2002 2:29 PM To: 'Mark Heintz PHP Mailing Lists'; [EMAIL PROTECTED] Subject: RE: [PHP] Creating arrays using results from MySQL query Mark, I'm still experiencing the same problem. Only one record is being added to the arrays. There are currently two records in the DB workgroups table matching the WHERE emp_id='$emp_login_id criteria. Here's the 2 records in the DB workgroups table: FIELDSValue#1 Value#2 wkgrp_id 1 4 grp_id111 222 emp_id39 39 wkgrp_create_date 2002-03-16 23:45:43 -00-00 00:00:00 wkgrp_change_date 2002-03-16 23:45:43 -00-00 00:00:00 wkgrp_change_by webmaster webmaster wkgrp_create_by webmaster webmaster role_id 1 1 I'm only getting back Value#2. Here's the modified PHP code: File: login.php if ($employee_2){ $emp_login_wkgrp_id = array(); $emp_login_grp_id = array(); $emp_login_role_id = array(); $i = 0; while($employee_2 = mysql_fetch_array($result_2)){ $emp_login_wkgrp_id[$i] = $employee_2[wkgrp_id]; $emp_login_grp_id[$i] = $employee_2[grp_id]; $emp_login_role_id[$i] = $employee_2[role_id]; $i++; } session_register('emp_login_wkgrp_id'); session_register('emp_login_grp_id'); session_register('emp_login_role_id'); } O From Now 'Till Then, \-Reginald Alex Mullin /\ 212-894-1690 -Original Message- From: Mark Heintz PHP Mailing Lists [SMTP:[EMAIL PROTECTED]] Sent: Monday, March 18, 2002 1:26 PM To: Mullin, Reginald Cc: [EMAIL PROTECTED] Subject:Re: [PHP] Creating arrays using results from MySQL query You have to call mysql_fetch_array for each record in your result set... $emp_login_wkgrp_id = array (); $emp_login_grp_id = array (); $emp_login_role_id = array (); $i = 0; while($employee_2 = mysql_fetch_array($result_2)){ $emp_login_wkgrp_id[$i] = $employee_2[wkgrp_id]; $emp_login_grp_id[$i] = $employee_2[grp_id]; $emp_login_role_id[$i] = $employee_2[role_id]; $i++; } mysql_fetch_array will return false when you run out of results, breaking the while loop. Check the manual for more info: http://www.php.net/manual/en/function.mysql-fetch-array.php mh. On Mon, 18 Mar 2002, Mullin, Reginald wrote: Hi Guys, I've been experiencing some problems when trying to build 3 arrays with the ID values of all of the groups a user belongs to. (I then want to register
RE: [PHP] Creating arrays using results from MySQL query
I should have been more exact in my original reply. The second query isn't necessary. Try this: File: login.php # if $employee_1, query db workgroups table to check if $emp_login_id belongs to any groups $sql_2 = SELECT * FROM workgroups WHERE emp_id='$emp_login_id'; $result_2 = mysql_query($sql_2) or die (mysql_error()); # initialize empty arrays $emp_login_wkgrp_id = array(); $emp_login_grp_id = array(); $emp_login_role_id = array(); $i=0; # while there are matches, set workgroups login variables in array, then # register workgroups login variables in session while($employee_2 = mysql_fetch_array($result_2)){ $emp_login_wkgrp_id[$i] = $employee_2[wkgrp_id]; $emp_login_grp_id[$i] = $employee_2[grp_id]; $emp_login_role_id[$i] = $employee_2[role_id]; $i++; } session_register('emp_login_wkgrp_id'); session_register('emp_login_grp_id'); session_register('emp_login_role_id'); mh -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php