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
Re: [PHP] Creating Arrays
Do you understand the options that the other people have explained? -- Rodney "Ashley M. Kirchner" wrote: "Rodney J. Woodruff" wrote: http://www.php.net/manual/en/function.msql-fetch-array.php Okay, call me dense. I can't figure this out. This is what I'm trying to do: $sql = "select p_id, project from proj where uid=$uid"; $result = mysql_db_query($database,$sql); (the resulting table in mysql is as follows: +--+---+ | p_id | project | +--+---+ |0 | Undefined | |1 | Work | |2 | Personal | +--+---+ 3 rows in set (0.00 sec) ...yes, that 'Undefined' IS a valid project, and the p_id's don't necessarily start at 0 either.) I need that result into the following: $items = array(0 = "Undefined", 1 = "Work", 2 = "Personal"); Reason is, I pass that $items variable to the following function: function MakeSelect($items, $selected) { $str = ""; while(list($value, $name) = each($items)) { $str .= "option value=\"$value\"" . ($value != $selected ? \ "" : " selected") . "$name\n"; } return $str; } ...which then creates (assuming the person had 'Work' previously selected): select name=whatever_i_specify option value="0"Undefined option value="1" selectedWork option value="2"Personal /select How do I create that $items array? AMK4 -- W | | I haven't lost my mind; it's backed up on tape somewhere. | ~ Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Laboratories, Inc. .eFax 248.671.0909 http://www.pcraft.com . 3550 Arapahoe Ave #6 .. . . . . Boulder, CO 80303, USA -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Creating Arrays
[posted and emailed] If you're looking for a simple way to work with databases I would use phplib. Phplib is a db abstraction layer which supports over a dozen databases including mysql. You can get the libraries at http://phplib.netuse.de/download/phplib-7.2c.tar.gz You will need to use "db_mysql.inc" I think using mysql specific functions is bad programming. Database specific calls should be abstracted out whenever possible. To do something like you would want to do you would use code like this: // create the db object $db = new DB_Sql; // initialize db parameters $this-Host = your host; $this-Database = db; $this-User = user name; $this-Password = password; // E.g. make a simple query $db-query("select username, uid, security_level from access where username='$f_username' and password='$f_password';"); $db-next_record(); $db_username = $db-f("username"); $si_userid = (int)($db-f("uid")); $si_security_level = (int)($db-f("security_level")); // putting the values in an array is trivial, but you should do it like this // or you will confuse your numeric ids with the ids that php puts in an array automatically $my_array["username"] = $db_username; $my_array["userid"] = $si_userid; On 11 Apr 2001 09:09:09 -0700, [EMAIL PROTECTED] ("Ashley M. Kirchner") wrote: I need to convert an MySQL result into an Array...somehow. The query returns the following: ++---+ | ID | Project | ++---+ | 1 | Home | | 2 | Work | | 3 | Family | | 4 |Misc. | | . | ... | | . | ... | ++---+ ...which I want to convert into: Array(1 = "Home", 2 = "Work", 3 = "Family", 4 = Misc.", etc); What's the best way to do this. AMK4 -- W | | I haven't lost my mind; it's backed up on tape somewhere. | ~ Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Laboratories, Inc. .eFax 248.671.0909 http://www.pcraft.com . 3550 Arapahoe Ave #6 .. . . . . Boulder, CO 80303, USA -- Jeff Greer - B.S. computer science - Univ. MO - Rolla - I do web hosting and development. Details at http://www.singlesconnection.org/services/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Creating Arrays
[posted and emailed] On 12 Apr 2001 12:48:52 -0700, [EMAIL PROTECTED] (Jeffrey Greer) wrote: // putting the values in an array is trivial, but you should do it like this // or you will confuse your numeric ids with the ids that php puts in an array automatically Whoops. I was wrong about array. Keys are only filled in automatically if you do this: $my_array[] = 'some value'; now $my_array[0] = 'some value'; -- Jeff Greer - B.S. computer science - Univ. MO - Rolla - I do web hosting and development. Details at http://www.singlesconnection.org/services/ -- Jeff Greer - B.S. computer science - Univ. MO - Rolla - I do web hosting and development. Details at http://www.singlesconnection.org/services/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Creating Arrays
http://www.php.net/manual/en/function.msql-fetch-array.php Hope this helps. "Ashley M. Kirchner" wrote: I need to convert an MySQL result into an Array...somehow. The query returns the following: ++---+ | ID | Project | ++---+ | 1 | Home | | 2 | Work | | 3 | Family | | 4 |Misc. | | . | ... | | . | ... | ++---+ ...which I want to convert into: Array(1 = "Home", 2 = "Work", 3 = "Family", 4 = Misc.", etc); What's the best way to do this. AMK4 -- W | | I haven't lost my mind; it's backed up on tape somewhere. | ~ Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Laboratories, Inc. .eFax 248.671.0909 http://www.pcraft.com . 3550 Arapahoe Ave #6 .. . . . . Boulder, CO 80303, USA -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Creating Arrays
try ?php while( $my_data = msql_fetch_row ( $your_query_identifier ){ $the_array_I_want[ $my_data[ 0 ] ] = $my_data[ 1 ] ; } echo "PRE"; print_r( $the_array_I_want ); echo "/PRE"; ? Be aware that adding an element to $the_array_I_want will give it the next sequential number which would look like an ID but have nothing to do with the contents of your database. Unless it is absolutely necessary I would recommend against doing this though. If you have record set with just a few records, no problem, 100's or 1000's of records and you should realize this array is using up your server memory. I am not sure of the mechanics of msql_fetch_row but I am pretty sure large result sets are not kept entirely in memory. Morgan At 12:10 PM 4/11/2001, you wrote: I need to convert an MySQL result into an Array...somehow. The query returns the following: ++---+ | ID | Project | ++---+ | 1 | Home | | 2 | Work | | 3 | Family | | 4 |Misc. | | . | ... | | . | ... | ++---+ ...which I want to convert into: Array(1 = "Home", 2 = "Work", 3 = "Family", 4 = Misc.", etc); What's the best way to do this. AMK4 -- W | | I haven't lost my mind; it's backed up on tape somewhere. | ~ Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Laboratories, Inc. .eFax 248.671.0909 http://www.pcraft.com . 3550 Arapahoe Ave #6 .. . . . . Boulder, CO 80303, USA -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Creating Arrays
"Rodney J. Woodruff" wrote: http://www.php.net/manual/en/function.msql-fetch-array.php Okay, call me dense. I can't figure this out. This is what I'm trying to do: $sql = "select p_id, project from proj where uid=$uid"; $result = mysql_db_query($database,$sql); (the resulting table in mysql is as follows: +--+---+ | p_id | project | +--+---+ |0 | Undefined | |1 | Work | |2 | Personal | +--+---+ 3 rows in set (0.00 sec) ...yes, that 'Undefined' IS a valid project, and the p_id's don't necessarily start at 0 either.) I need that result into the following: $items = array(0 = "Undefined", 1 = "Work", 2 = "Personal"); Reason is, I pass that $items variable to the following function: function MakeSelect($items, $selected) { $str = ""; while(list($value, $name) = each($items)) { $str .= "option value=\"$value\"" . ($value != $selected ? \ "" : " selected") . "$name\n"; } return $str; } ...which then creates (assuming the person had 'Work' previously selected): select name=whatever_i_specify option value="0"Undefined option value="1" selectedWork option value="2"Personal /select How do I create that $items array? AMK4 -- W | | I haven't lost my mind; it's backed up on tape somewhere. | ~ Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Laboratories, Inc. .eFax 248.671.0909 http://www.pcraft.com . 3550 Arapahoe Ave #6 .. . . . . Boulder, CO 80303, USA -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Creating Arrays
On Wed, Apr 11, 2001 at 03:55:38PM -0600, Ashley M. Kirchner wrote: "Rodney J. Woodruff" wrote: http://www.php.net/manual/en/function.msql-fetch-array.php (snip) I need that result into the following: $items = array(0 = "Undefined", 1 = "Work", 2 = "Personal"); Here's a hint. $arr[$key] = $value; Define $key and $value from the row data that mysql_fetch_array gives you. Reason is, I pass that $items variable to the following function: function MakeSelect($items, $selected) { $str = ""; while(list($value, $name) = each($items)) { $str .= "option value=\"$value\"" . ($value != $selected ? \ "" : " selected") . "$name\n"; } return $str; } For the record, you're thinking about list() backwards. In your function, the array keys will go into $value, and the values will go into $name. HTH, Matt -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]