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