Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2002
    Location
    Cube Galaxy HQ
    Posts
    104

    Simple MySQL Question

    If I want to use SELECT to get the last 10 entries of a database, what would I do?
    :-D

  2. #2
    Join Date
    Apr 2003
    Location
    London, UK
    Posts
    4,721
    SELECT 'item' FROM 'table' ORDER BY id DESC LIMIT 10
    Hyperconfused (™)

  3. #3
    Join Date
    Apr 2002
    Location
    Cube Galaxy HQ
    Posts
    104
    Is there anything wrong with this code:

    PHP 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
    :-D

  4. #4
    Join Date
    Apr 2003
    Location
    Los Angeles, CA
    Posts
    820
    > 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?

  5. #5
    Join Date
    Aug 2002
    Location
    Southwest Michigan
    Posts
    935
    PHP Code:
     <?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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •