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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:28:51+00:00 2026-05-27T22:28:51+00:00

I’m building a custom database class for use with my projects. I’ve built a

  • 0

I’m building a custom database class for use with my projects. I’ve built a fetchAssoc() method, however when I call it within a while loop, it’s not moving to the next record. It calls the first record over and over until the script times out.
Below is the relevant code:

METHODS:

function runQuery($q)
{
    $this->numQueries++;
    $this->query = ($q);
    $this->setResult($q);
    $this->result;  
}

function fetchAssoc($q = NULL)
{
    if($q == NULL)
    {
        $q = $this->query;  
    }
    $this->setResult($q);
    if($q == NULL  || mysql_num_rows($this->result) < 1)
    {
        return NULL;    
    }
    else 
    {
        return mysql_fetch_assoc($this->result);
    }
}
    function setResult($q = NULL)
{
    if($q == NULL)
    {
        $q = $this->query;  
    }
    if($q == NULL)
    {
        return FALSE;   
    }
    else
    {
        $this->result = @mysql_query($q);   
    }
}

SCRIPT:

//runQuery -- Should run the query and store the Result and Query
$q = "SELECT * FROM make ORDER BY make";
$db->runQuery($q);

//fetchAssoc --  return current row of result set and move pointer ahead
foreach($db->fetchAssoc() as $key => $value)
{
echo $value." has a foreign key of: ".$key."<br />";    
}
//Also tried
while($row = fetchAssoc())
{
    echo $value." has a foreign key of: ".$key."<br />";    
}
  • 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-27T22:28:52+00:00Added an answer on May 27, 2026 at 10:28 pm

    That is because you execute the query each time you call the fetchAssoc function (at least that is what I think setResult is supposed to be doing when looking at your code). After the query is reset you return the first association from the result which results in an array. Because it keeps resulting in the first association of your result set the code keeps looping until max_execution time is reached.

    fetchAssoc should be doing nothing more then return mysql_fetch_assoc for this->result if I understand your code correct.

    I will break it down for you:

    //first lines of fetchAssoc
    if($q == NULL)
    {
        $q = $this->query;  
    }
    

    in the piece of code using this function you pass no $q so $q is always $this->query.

    $this->setResult($q);
    

    You then call setResult which according to your own comment performs the query and sets this->result. So if you call the fetchAssoc function, $this->query is executed every time and result is refreshed with the result for that query every time.

    if($q == NULL  || mysql_num_rows($this->result) < 1)
    {
        return NULL;    
    }
    else 
    {
        return mysql_fetch_assoc($this->result);
    }
    

    Since $q can never be null (you gave a value to it in that case earlier) the only check here is on num_rows. As long as that is the case you return the first row of $this->result with fetch_assoc. This is always the same row since you refresh the query and the result as well each call.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a reasonable size flat file database of text documents mostly saved in
I am doing a simple coin flipping experiment for class that involves flipping a

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.