开发者

mysql_fetch_array() timing out

I am trying to query a database, but it seems to just load for an age and not do anything. It's a simple query and shouldnt take longer than a millisecond.

while($row = mysql_fetch_array(getWallPosts($userid)))
{
}

Now when I replace this code with:

echo mysql_num_rows(getWallPosts($userid));

It just displays '1' in fact there's only one record in the DB and it's just a simple SELECT statement.

Here's the getWallPosts function:

function getWallPosts($userid, $limit = "10")
{
    $result = dbquery("SELECT * FROM commentpost 
                           WHERE userID = ".$userid." 
                           ORDER BY commentPostID DESC LIMIT 0, ".$limit);
    return $result;开发者_JAVA技巧
}

Also, when I put the SQL string that it's executing into MySQL's query browser. It takes no time at all.

Any ideas?


You appear to be looping indefinitely because you're retrieving a new set (one record) of data each time.

$result = getWallPosts($userid);

while ($row = mysql_fetch_array($result)) {
    //printf($row[0]);  
}


You need to get the data once and loop through it. Your code is getting the data, running the loop and then getting the data again.

Try:

$posts = getWallPosts($userid);
while($row = mysql_fetch_array($posts))
{
  //Code to execute
}


Its an infinite loop. The expression in the while always executes so it will always be true.


You're returning a new result set each time the while statement executes. You should call getWallPosts first, assign it to $results, and then loop over it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜