我正在構建自定義數據庫類以供我的項目使用。我已經構建了一個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