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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:06:36+00:00 2026-05-15T07:06:36+00:00

Assuming that the mysqli object is already instantiatied (and connected) with the global variable

  • 0

Assuming that the mysqli object is already instantiatied (and connected) with the global variable $mysql, here is the code I am trying to work with.

class Listing {
private $mysql;
function getListingInfo($l_id = "", $category = "", $subcategory = "", $username = "", $status = "active") {
        $condition = "`status` = '$status'";
        if (!empty($l_id)) $condition .= "AND `L_ID` = '$l_id'";
        if (!empty($category)) $condition .= "AND `category` = '$category'";
        if (!empty($subcategory)) $condition .= "AND `subcategory` = '$subcategory'";
        if (!empty($username)) $condition .= "AND `username` = '$username'";
        $result = $this->mysql->query("SELECT * FROM listing WHERE $condition") or die('Error fetching values');
        $this->listing = $result->fetch_array() or die('could not create object');
        foreach ($this->listing as $key => $value) :
            $info[$key] = stripslashes(html_entity_decode($value));
        endforeach;
        return $info;
    }
}

there are several hundred listings in the db and when I call $result->fetch_array() it places in an array the first row in the db.
however when I try to call the object, I can’t seem to access more than the first row.
for instance:
$listing_row = new Listing;
while ($listing = $listing_row->getListingInfo()) {
echo $listing[0];
}

this outputs an infinite loop of the same row in the db. Why does it not advance to the next row?
if I move the code:

$this->listing = $result->fetch_array() or die('could not create object');
        foreach ($this->listing as $key => $value) :
            $info[$key] = stripslashes(html_entity_decode($value));
        endforeach;

if I move this outside the class, it works exactly as expected outputting a row at a time while looping through the while statement.
Is there a way to write this so that I can keep the fetch_array() call in the class and still loop through the records?

  • 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-15T07:06:36+00:00Added an answer on May 15, 2026 at 7:06 am

    Your object is fundamentally flawed – it’s re-running the query every time you call the getListingInfo() method. As well, mysql_fetch_array() does not fetch the entire result set, it only fetches the next row, so your method boils down to:

    1. run query
    2. fetch first row
    3. process first row
    4. return first row

    Each call to the object creates a new query, a new result set, and therefore will never be able to fetch the 2nd, 3rd, etc… rows.

    Unless your data set is “huge” (ie: bigger than you want to/can set the PHP memory_limit), there’s no reason to NOT fetch the entire set and process it all in one go, as shown in Jacob’s answer above.

    as a side note, the use of stripslashes makes me wonder if your PHP installation has magic_quotes_gpc enabled. This functionality has been long deprecrated and will be removed from PHP whenever v6.0 comes out. If your code runs as is on such an installation, it may trash legitimate escaping in the data. As well, it’s generally a poor idea to store encoded/escaped data in the database. The DB should contain a “virgin” copy of the data, and you then process it (escape, quote, etc…) as needed at the point you need the processed version.

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

Sidebar

Related Questions

I'm trying to unpickle an object stored as a blob in a MySQL database.
searching a photo by dominant colors using mysql is quite simple. assuming that the
Assuming that writing nhibernate mapping files is not a big issue....or polluting your domain
Assuming that I want to use a hash as an ID instead of a
Am I correct in assuming that the only difference between "windows files" and "unix
Am I correct in assuming that I always need to explicitly deploy referenced assemblies
How do you go about building a complete keyboard-accessible web application? Assuming that this
Assuming the you are implementing a user story that requires changes in all layers
Assuming a series of points in 2d space that do not self-intersect, what is
Assuming a fluid layout is not an option (since that is a different discussion

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.