PHP/MYSQL - Mysqli Prepared Statements - Fetch table data and add each row to a local array? -


I have a list of different postal information This data is within a mysql table called "mailbox".

I would like to add each row of this table to a local array for use in a script.

I'm facing a problem cap that I'm getting errors related to undefined variables inside the loop.

Even I have the code:

  // Get the mail information and create array $ postalCodes in [] = array (); If ($ stmt = $ link-> ("SELECT id, suburb, boxcode, street code, city name by mail")} {if (! $ Stmt-> execute ()) {printf ("Failed to execute"); } If (! $ Stmt- & gt; bind_result ($ id, $ suburb, $ boxcode, $ street code, $ city name)) {printf ("failed to bind parameters"); } If (! $ Stmt-> store_result ()) {printf ("Failed to store results"); } While ($ stmt-> fetch ()) {$ postalCodes ['id'] + = $ id; $ Postcode ['suburb'] + = $ suburb; $ Postcode ['boxcode'] + = $ boxcode; $ Postcode ['road code'] + = $ road code; $ Postcode ['cityname'] + = $ city name; } $ Stmt- & gt; Close (); } Foreach ($ postalCodes $ postalCode) {if ($ postalCode ['boxCode'] == 5850) {printf ("{$ postalCode ['suburb']}"); }}   

Anyone can spot the problem with this code or I could suggest a way to code this process better.

Any help and insights, this relationship will be highly appreciated, thanks!

"error" is probably due to your associative array:

  $ Postcode ['id'] + = $ id;   

Ask yourself: What is the value of the $ postalCodes ['id'] which you call the first time? If it is still not defined then you can start the variable already, or you can stop such warnings.

Edit: array-issue

Do something you want:

  $ postalCodes = array () ; $ ($ Stmt-- gt; fetch ()) {$ postalCodes [] = Array ('id' = & gt; $ id, 'suburb' = & gt; $ suburb, 'boxcode' = & gt; $ boxCode, 'Streetcode' = & gt; $ road code, 'townName' => $ cityName); } Foreach ($ postalCodes $ as postalCode) {// You need to do with the associative array, e.g. $ PostcalCode echo ['boxcode']; }   

I think that you were confused about the meaning of + = if you want to add an element to your element, then you The array uses [] = some .

Comments