FW: [PHP] Creating arrays using results from MySQL query

2002-03-18 Thread Mullin, Reginald

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
  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: FW: [PHP] Creating arrays using results from MySQL query

2002-03-18 Thread Jason Wong

On Tuesday 19 March 2002 03:44, Mullin, Reginald wrote:
 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.

I don't know what the status of your code is now. But you're probably doing:


// read the first record
$employee_2 = mysql_fetch_array($result_2);
...
...
...
 if ($employee_2){
$emp_login_wkgrp_id = array();
$emp_login_grp_id = array();
$emp_login_role_id = array();
$i = 0;
// reading the rest of the records
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++;
}


Thus your first record is 'lost' (not displayed)


-- 
Jason Wong - Gremlins Associates - www.gremlins.com.hk

/*
Why would anyone want to be called Later?
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php