PDA

View Full Version : Looping mysql_fetch_assoc


crays
06-10-2008, 02:45 PM
Hi, i'm trying to loop my news section until the condition is met. But the code tells me

mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in ...

here is my code:


<?php
//connected to the db
$result = mysql_query("SELECT TOP 5 * FROM news ORDER BY ID DESC");

while ($row = mysql_fetch_assoc($result))
{
?>
<table>
Title : <?php echo $row['title'] ?>
Content : <?php echo $row['content'] ?>
</table>
<?php
}
?>


Thanks~

Brandoe85
06-10-2008, 03:08 PM
There is an error in your query so it's failing....add an or die to your query to debug:
mysql_query() or die(mysql_error());
Also, always check to make sure there is a result....
if($result)
Or however you like.


mysql doesn't have TOP like sql server does, you want LIMIT.
http://dev.mysql.com/doc/refman/5.0/en/select.html

idalatob
06-10-2008, 03:17 PM
Try changing your code to this:


//old
$result = mysql_query("SELECT TOP 5 * FROM news ORDER BY ID DESC");
//new
$result = mysql_query("SELECT * FROM news ORDER BY ID DESC LIMIT 0,5");

crays
06-11-2008, 09:33 AM
Oh!! Thanks for the advice. I didn't know they didn't accept TOP 5. Thanks~