我正在构建自定义数据库类以供我的项目使用。我已经构建了一个fetchAssoc()方法,但是当我在while循环内调用它时,它不会移动到下一个记录。它一次又一次地调用第一条记录,直到脚本超时。
下面是相关代码:PHP MYSQL数据库类 - 获取mysql_fetch_assoc方法移至下一条记录
方法:
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 />";
}
我也尝试过:while($ row = fetchAssoc()),但它一遍又一遍地返回相同的结果 – Stewie 2012-01-06 16:12:02
你可以发布'setResult'方法吗? – 2012-01-06 16:14:52
功能的setResult($ Q = NULL) \t { \t \t如果($ Q == NULL) \t \t { \t \t \t $ Q = $这个 - >查询; \t \t } \t \t 如果\t($ Q == NULL) \t \t { \t \t \t返回FALSE; \t \t } \t \t 其他\t \t {\t \t \t \t $这 - >结果= @mysql_query($ Q); \t \t \t} \t} – Stewie 2012-01-06 16:15:37