php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72211 Associative array returns not = Numeric array
Submitted: 2016-05-12 22:07 UTC Modified: 2016-05-13 00:12 UTC
From: 4OtherBusiness at comcast dot net Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.6Git-2016-05-12 (snap) OS: Windows 7 Enterprise
Private report: No CVE-ID: None
 [2016-05-12 22:07 UTC] 4OtherBusiness at comcast dot net
Description:
------------
Inside my PHP do_mysql_multi() funct that does querying, I kept getting nonsense until I put this first echo just below the while () shown here.

(Using newer mysqli not mysql functions.)

Basically the associative value is wrong, the numeric indexed value is correct for first element of first (only) row.

Putting:     echo "<pre>"; print_r($row) ;

Shows the nonsense when placed after the test for result = mysqli_store_result($link)
            

75 columns expected c/o SELECT tbl1.*, tbl2.*, tbl3.*, tbl4.*, tbl5.* ....

First few fields (of the 1 return row) shown here.  Notice first element.  Should be = 101, (= where clause value)

Array
(
    [0] => 101
    [modem_config_id] => 1
    [1] => 1
    [modem_activity_id] => 1
    [2] => 1
    [customer_id] => 1
    [3] => 1
    [aircraft_id] => 1
    [4] => 1
    [intl_rate_plan_id] => 1
    [5] => 1
    [dom_rate_plan_id] => 1
    [6] => U064672
    [serial_number] => U064672
    
    ... All the rest are good

Test script:
---------------
if (mysqli_multi_query($link,$query ))
    { 
      do
      { 
        
        if ($result = mysqli_store_result($link))
        { 
          $row_nbr=0 ;
          while ($row = mysqli_fetch_array($result, MYSQLI_BOTH))
          { 
            $row_nbr++ ;
            for ($field_nbr=1;$field_nbr<=$fields_returned;$field_nbr++)
            { 
              $values[$row_nbr][$field_names[$field_nbr]]    =$row[$field_nbr-1] ; 
            }
          }
          
          mysqli_free_result($result) ;
          
          
        }  // EO if ($result = mysqli_store_result($link))
        
      } while (mysqli_more_results($link) && mysqli_next_result($link)) ;
      
    }  // EO  if (mysqli_multi_query($link,$query ))
    else 
    {
      // This will be true if the 1st query failed
      if ($query_nbr == 0)
      { 
        $result_vector1['Error'] = "MySQL Error #:  ".mysqli_errno($link).":  ".mysqli_error($link) ;
        $result_vector2['Error'] = "MySQL Error in first query." ;
      }
      
    }  // EO MySQL
    

Expected result:
----------------
I expect [0] value to equal ['modem_config_id'] value.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-12 22:15 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2016-05-12 22:15 UTC] requinix@php.net
Are there 149 or 150 elements in the $row array? Does your query return more than one field named "modem_config_id"?
 [2016-05-12 22:52 UTC] 4OtherBusiness at comcast dot net
-Status: Feedback +Status: Open
 [2016-05-12 22:52 UTC] 4OtherBusiness at comcast dot net
Looking more closely there are more oddities.
I'll break the output below by table.
Only 1 occurrence of modem_config_id IS REPORTED OUT,
though it and other FK columns are repeated.

How does one go about this w/o enumerating EVERY column AND aliasing them
(with a SUPER TON of code editing in that case)?


SELECT mc.*, ma.*, c.*, a.*, drp.*, irp.*, n.note 
FROM ModemConfig mc 
JOIN ModemActivity ma USING (modem_activity_id) 
JOIN Customer c USING(customer_id ) 
JOIN Aircraft a USING(aircraft_id ) 
JOIN DomRatePlan drp USING(dom_rate_plan_id ) 
JOIN IntlRatePlan irp USING(intl_rate_plan_id ) 
JOIN Note n ON mc.note_id = n.note_id 
WHERE mc.modem_config_id=101 ;


Array
(
    [0] => 101
    [modem_config_id] => 1
    [1] => 1
    [modem_activity_id] => 1
    [2] => 1
    [customer_id] => 1
    [3] => 1
    [aircraft_id] => 1
    [4] => 1
    [intl_rate_plan_id] => 1
    [5] => 1
    [dom_rate_plan_id] => 1
    [6] => U064672
    [serial_number] => U064672
    [7] => 
    [part_number] => 
    [8] => 3549 7604 0474 374
    [IMEI] => 3549 7604 0474 374
    [9] => 10.145.10.134
    [IP_addr] => 10.145.10.134
    [10] => RD-AA8104-02
    [assembly_id] => RD-AA8104-02
    [11] => 3G
    [type] => 3G
    [12] => 2014-03-28
    [date_activation] => 2014-03-28
    [13] => 
    [date_into_service] => 
    [14] => 
    [date_ship_confirm] => 
    [15] => 310410656198630@mascorp.com
    [user_id] => 310410656198630@mascorp.com
    [16] => A036D3061516A440
    [password] => A036D3061516A440
    [17] => 8901 4105 2765 6198 6303
    [simm_identifier] => 8901 4105 2765 6198 6303
    [18] => 4252053401
    [wireless_no] => 4252053401
    [19] => 310 410 656 198 630
    [IMSI] => 310 410 656 198 630
    [20] => 2016-05-12 13:02:14
    [date_mc_record_created] => 2016-05-12 13:02:14
    [21] => 122                                 <== last field in ModemConfig is 'note_id', WRONG associate value
    [note_id] => 4

    [22] => 1                                   <== ID correct for ModemActivity table ma
    [23] => 1                                   <== FK 'modem_config_id' in ModemActivity table
    [24] => 2016-05-12 13:01:59                 <== 3rd column in ModemActivity  CORRECT
    [date_ma_created] => 2016-05-12 13:01:59
    [25] => Unknown
    [modem_location] => Unknown
    [26] => Unknown
    [modem_utilization] => Unknown
    [27] => Other
    [billable_status] => Other
    [28] => 
    [date_current_status_began] => 
    [29] => 
    [date_suspended] => 
    [30] => 
    [date_suspend_renew] => 
    [31] => 0
    [confgd_for_roaming] => 0
    [32] => 16                      <== Another note_id numeric index value and missing associative value
                                 
                                    <== Missing 'customer_id'=1
    [33] => 1
    [34] => Unassigned Customer
    [cust_name] => Unassigned Customer
    [35] => ??
    [cust_desig_2char] => ??
    [36] => ???
    [cust_desig_3char] => ???
    [37] => None
    [region] => None
    [38] => 9                      <== Missing 'note_id' for last field in customer (value=9 CORRECT)
     
    [39] => 1                          <== Aircraft.aircraft_id
    [40] => 1                          <== Aircraft.customer_id
    [41] => Unassigned
    [aircraft_reg_no] => Unassigned
    [42] => Unassigned
    [aircraft_type] => Unassigned
    [43] => Unassigned
    [ife_system_type] => Unassigned
    [44] => Unassigned
    [program_number] => Unassigned
    [45] => Unknown
    [aircraft_status] => Unknown
    [46] => 10                             <== Aircraft.note_id=10

    [47] => 1                                     <== DomRatePlan.dom_rate_plan_id    CORRECT
    [48] => 1                                     <== DomRatePlan.service_provider_id CORRECT
    [service_provider_id] => 1
    [49] => Unassigned Dom Plan Name
    [domplan_name] => Unassigned Dom Plan Name
    [50] => Unassigned Dom Plan Code
    [domplan_code] => Unassigned Dom Plan Code
    [51] => 0.00
    [domplan_rate_charge] => 0.00
    [52] => 0
    [domplan_limit_GB] => 0
    [53] => 0.00
    [domplan_overage_charge] => 0.00
    [54] => 0
    [domplan_overage_charge_for_XX_GB] => 0
    [55] => 180
    [suspension_period] => 0
    [56] => 
    [ident_of_latest_drp_invoice] => 
    [57] => 0
    [number_modems_now_on_domplan] => 0
    [58] => 2040-12-31
    [date_no_more_drp_contracts] => 2040-12-31
    [59] => 3                                    <== DomRatePlan.note_id   CORRECT

    [60] => 1                                    <== IntlRatePlan.intl_rate_plan_id =1 CORRECT
    [61] => 1                                    <==             .service_provider CORRECT
    [62] => Unassigned/No Intl Name
    [intlplan_name] => Unassigned/No Intl Name
    [63] => Unassigned/No Intl Code
    [intlplan_code] => Unassigned/No Intl Code
    [64] => 0.00
    [intlplan_rate_charge] => 0.00
    [65] => 0
    [intlplan_limit_MB] => 0
    [66] => 0.00
    [intlplan_overage_rate_per_KB] => 0.00
    [67] => 0
    [68] => 0
    [intlplan_upgrade_at_MB_SameCarrier] => 0
    [69] => 0
    [intlplan_upgrade_at_MB_BestRate] => 0
    [70] => Jan 1970
    [ident_of_latest_irp_invoice] => Jan 1970
    [71] => 0
    [number_modems_now_on_intlplan] => 0
    [72] => 2040-12-31
    [date_no_more_irp_contracts] => 2040-12-31
    [73] => 4                                      <== IntlRatePlan.note_id = 4 CORRECT

    [74] => Modem added care of May 11 2016 Structures Manuf List Manufacturing list import.       <== Note.note  (my screwy message to fix)
    [note] => Modem added care of May 11 2016 Structures Manuf List Manufacturing list import.
)
 [2016-05-12 22:54 UTC] 4OtherBusiness at comcast dot net
0 .. 74, numeric and 75 associative.
 [2016-05-13 00:12 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-05-13 00:12 UTC] requinix@php.net
I count 74 numeric and 60 associative.

Array keys must be unique - there cannot be more than one value using the same key. Your query is returning multiple sets of fields with the same name so most of those associative values will be lost.

> How does one go about this w/o enumerating EVERY column AND aliasing them
> (with a SUPER TON of code editing in that case)?
You don't. Not in any practical way. Something will have to give.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC