Web Hosting Talk







View Full Version : Simple MySQL Question


Falco1199
09-01-2003, 05:23 PM
If I want to use SELECT to get the last 10 entries of a database, what would I do?

Loon
09-01-2003, 05:46 PM
SELECT 'item' FROM 'table' ORDER BY id DESC LIMIT 10

Falco1199
09-01-2003, 06:23 PM
Is there anything wrong with this code:

<?php
include("includes/database.php"); // connects to database
print("<html><body>");
$query = "SELECT * FROM site_news ORDER BY ID DESC LIMIT 10";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
print($row['body'] . "<br /><br />");
}
print("</body></html>");
?>

?

I get this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/virtual/site141/fst/var/www/html/db_test1.php on line 6

luki
09-01-2003, 07:36 PM
> Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

It looks like the query failed; check mysql_error(). Perhaps you don't have an column named ID in your site_news table?

FW-Mike
09-01-2003, 11:45 PM
<?php
include("includes/database.php"); // connects to database
print("<html><body>");
$result = mysql_query("SELECT * FROM site_news ORDER BY ID DESC LIMIT 10") or die("Error in statement! ".mysql_error());
while($row = mysql_fetch_array($result))
{
print($row['body'] . "<br /><br />");
}
print("</body></html>");
?>


Get in a habit of doing thaty for your statements. WIll help you in debugging