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 3939904
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:21:16+00:00 2026-05-20T00:21:16+00:00

This is my first question here on StackOverflow, and quite frankly I’m fairly new

  • 0

This is my first question here on StackOverflow, and quite frankly I’m fairly new to PHP. Just to give you a brief heads-up 😉

I’m building an OOP-based website, in a 3-tier architecture. In my data abstraction layer, I have an object I called DbAdapter, which contains all functions necessary to communicate with the database. One of these functions is the one below: read($sql), which takes an SQL query and stores the result in a two-dimensional array.

For this, it uses two nested for-loops (one for the rows and one for the columns per row). And while the iterator $i increments as usual, somehow the last element of the array is overwritten.

I have absolutely no idea how this is possible, so the mistake I made must be extremely stupid.

Anyone care to help out a newbie?

Thanks in advance, Sam

public $loadedRows;
public function read($sql)
{
    if ($this->connect())
    {
        $result = mysql_query($sql);
        if ($result)
        {
            $totalRows = mysql_num_rows($result);
            $totalFields = mysql_num_fields($result);

            for ($i = 0; $i < $totalRows; $i++)
            {
                for ($j = 0; $j < $totalFields; $j++)
                {
                    $fieldName = mysql_field_name($result, $j);
                    $loadedFields["$fieldName"] = mysql_result($result, $i, $fieldName);
                }

                $this->loadedRows[i] = $loadedFields;
            }

            $this->closeConnection();
            return $this->loadedRows;
        }
    }
}
  • 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-05-20T00:21:17+00:00Added an answer on May 20, 2026 at 12:21 am

    you just forgot $ before i $this->loadedRows[$i]

    and this code should be way shorter:

    public function read($sql)
    {
        $a = array();
        $result = mysql_query($sql);
        if ($result)
        {
            while($row = mysql_fetch_assoc($res)) $a[]=$row;
        }
        return $a;
    }
    

    That’s ALL.

    and to catch these errors yourself, you should set error reporting level to E_ALL
    to do that you can add this line

    error_reporting(E_ALL);
    

    in your config file.
    It will tell PHP to watch such mistakes (using undefined constant i in this case) and notify you

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

Sidebar

Related Questions

Greetings to all! This is my first question here on stackoverflow. I have a
this is my first question to stackoverflow so here it goes... I use cruise
Greetings everyone. This is my first question here at stackoverflow so please bear with
this is my first question here @stackoverflow. I'm writing a monitoring tool for some
This is my first question here on stackoverflow, so I hope that I am
this is my first question here so I hope I can articulate it well
Okay this is a fairly broad question. This is my first App and I'm
this is my first question on StackOverflow, but I think that we'll both come
Am just learning C and this is my first time on stackoverflow so am
This is my first question so please be patient :) Background: I'm implementing an

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.