2013-01-24 52 views
1
$sql = "SELECT `description`,`auction_id` FROM `auctions` WHERE `description` REGEXP '(97(8|9))?[[:digit:]]{9}([[:digit:]]|X)'"; 
$result = mysql_query($sql); 

while ($row = mysql_fetch_assoc($result)) { 
$row = mysql_fetch_row($result); 
echo "$row[1]"; 

是否有任何理由爲什麼echo "$row[1]";返回隔行?我得到的結果是這樣建議的,我試圖得到每一行mysql_fetch_row每隔一行返回一次?

+0

是否正確查詢工作在phpMyAdmin? –

+0

那麼,小小的結論(那不怪別人)是「代碼是*丟棄*其他記錄」 - 爲什麼?注意副作用:D – 2013-01-24 08:39:07

+3

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 –

回答

0

您正在調用fetch函數兩次,因此它似乎每次跳過一行。

while ($row = mysql_fetch_assoc($result)) { 
$row = mysql_fetch_row($result); //This is not needed 
echo "$row[1]"; 
} 

應該

while ($row = mysql_fetch_assoc($result)) { 
echo $row[1]; 
} 
+0

由於某種原因返回空白結果 – user1973125

+0

不知道爲什麼會被拒絕投票。這是錯的嗎? –

0

而是具有循環爲:

while ($row = mysql_fetch_assoc($result)) { 
    $row = mysql_fetch_row($result); 

使用:

while ($row = mysql_fetch_assoc($result)) { 
0

你應該使用這樣。

while ($row = mysql_fetch_assoc($result)) { 
    $desc[] = $row['description']; 
} 
+0

也許補充說明爲什麼這是正確的方法? – Maerlyn

2

要真正回答你的問題,爲什麼它發生

$row = mysql_fetch_assoc($result); //this advances the internal data pointer by 1 
$row = mysql_fetch_row($result); //this advances it by 1 more 

兩者都在循環的每個迭代執行。 你應該使用一個,而不是兩個。

另外,您不應該在新代碼中使用mysql_ *函數,因爲它已被棄用。按照註釋中的說明使用PDO或MySQLi。

+0

非常感謝! – user1973125

+0

@ user1973125請閱讀http://meta.stackexchange.com/questions/16721/how-does-accept-rate-work/65088#65088 –

0

如果在phpMyAdmin查詢正常工作,然後要麼你可以使用

while ($row = mysql_fetch_assoc($result)) { 
echo $row['description']; 
} 

或者

$row = mysql_fetch_row($result); 
echo $row[1]; 

while ($row = mysql_fetch_array($result)) { 
echo $row['description']; //or 
echo $row[1]; 
}