0

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

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

Any ideas?

1
  • change $result = dbquery("SELECT * FROM commentpost WHERE userID = ".$userid." ORDER BY commentPostID DESC LIMIT 0, ".$limit); to $result = dbquery("SELECT * FROM commentpost WHERE userID = '".$userid."' ORDER BY commentPostID DESC LIMIT 0, ".$limit); otherwise you may suffer SQL-injection attacks (depending on where $userid came from, also note that if $limit comes from outside, there's NO WAY to protect against SQL-injection. If it's from inside you're safe Commented May 4, 2011 at 22:11

4 Answers 4

3

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]);  
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1. Try: $r = getWallPosts($userid); while($row = mysql_fetch_array($r)) {}
1

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
}

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.