PHP MYSQL数据库类 - 获取mysql_fetch_assoc方法以移动到下一条记录

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 />";    
}

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.