drbl91357 2012-01-06 16:10
浏览 98
已采纳

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 />";    
}
  • 写回答

1条回答 默认 最新

  • dqy0707 2012-01-06 16:15
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?