Log in

View Full Version : Database class query



jonnyynnoj
10-17-2007, 02:55 PM
So I just started OOP yesterday and up until this point all my other functions worked well.
However, I now have a problem where the query loops forever when using mysql_fetch_array.

The function:
function get_posts($forums='', $limit='', $order='')
{
if ($forums!=''){
$forums_value = ' WHERE forum_id IN ('.$forums.')';
}
if ($order!=''){
$order_value = ' ORDER BY '.$order;
}
if ($limit!=''){
$limit_value = ' LIMIT '.$limit;
}

$this->query("SELECT * FROM ".$this->table_prefix."posts".$forums_value.$order_value.$limit_value."");
return $this->fetch_array();
}

The query function:
function query($query, $cache=TRUE)
{
$this->numqueries++;
if ($cache==TRUE)
{
$this->lastresult = mysql_query($query);
return $this->lastresult;
}
else
{
return mysql_query($query);
}

}

The fetch_array function:
function fetch_array($result='')
{
$this->numqueries++;
if ($result=='')
{
return mysql_fetch_array($this->lastresult);
}
else {
return mysql_fetch_array($result);
}
}

Using the below in index.php:
while ($row = $bbsdk->get_posts()){
echo $row['time'].'<br />';
}

Gives me:
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
1192627075
etc..
etc..

There are 4 rows in the database table, and only 1 of them has the time: 1192627075.

So for some reason, its looping forever but only getting the first row.

Any help? :confused:

Twey
10-17-2007, 03:56 PM
Because you call query() in get_posts(), which resets the query each time the loop iterates. You're reinventing the wheel; I suggest looking into Propel (http://propel.phpdb.org/trac/).