Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8210005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:55:48+00:00 2026-06-07T09:55:48+00:00

I have a list of various postal information. This data is within a mysql

  • 0

I have a list of various postal information. This data is within a mysql table called “postal_codes”.

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

The problemt hat I am facing is that I keep getting errors relating to undefined variables within the while loop.

Here is the code I have so far:

// Get postal info and make into array
$postalCodes[] = array();
if ($stmt = $link->prepare("SELECT id, suburb, boxCode, streetCode, townName FROM postal_codes")) {

    if (!$stmt->execute())
    {
        printf("failed to execute");
    }
    if (!$stmt->bind_result($id, $suburb, $boxCode, $streetCode, $townName))
    {
        printf("failed to bind params");
    }
    if (!$stmt->store_result())
    {
        printf("failed to store result");
    }
    while ($stmt->fetch())
    {
        $postalCodes['id'] += $id;
        $postalCodes['suburb'] += $suburb;
        $postalCodes['boxCode'] += $boxCode;
        $postalCodes['streetCode'] += $streetCode;
        $postalCodes['townName'] += $townName;
    }
    $stmt->close();
}

foreach ($postalCodes as $postalCode)
{
    if ($postalCode['boxCode'] == 5850)
    {
        printf("{$postalCode['suburb']}");
    }
}

Could someone by chance spot the problem with this code or suggest a way for me to code this procedure in a better way.

Any assistance and insight in this regard would be greatly appreciated, thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:55:50+00:00Added an answer on June 7, 2026 at 9:55 am

    The “error” is probably caused by your associative array:

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

    Ask yourself: what is the value of $postalCodes['id'] the first time you call this? It still isn’t defined. So you can either initialize the variables beforehand, or you can switch off warnings of this type through error_reporting().

    EDIT: The array-issue

    You want to do something like:

    $postalCodes=array();
    while ($stmt->fetch()) {
        $postalCodes[] = array('id' => $id,
            'suburb' => $suburb,
            'boxCode' => $boxCode,
            'streetCode' => $streetCode,
            'townName' => $townName);
    }
    foreach($postalCodes as $postalCode) {
        // do whatever you need to do
        // with the associative array, e.g.
        echo $postcalCode['boxCode'];
    }
    

    I think you were confused about the meaning of +=. If you want to add an element to your array, you use array[]=something.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have a list of rough rule-of-thumb estimators for the various data structures?
I have a List that has various derived classes. I may have something like
I would like to be able to store various static methods in a List
I have a list of lists that looks like this: [['Tom', 'Dick'], ['Harry', 'John',
I have a list which contains various docIds. When the information is to be
I have a button which opens a context menu with a list of various
Here is the problem: I have a list of items with various length for
I have a main select list of courses which drives various things on a
I have list of input area in the form with id like contact1_title, contact2_title,
I have list of articles on the page in table and i want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.