Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts

    MY SQL fetching results

    Hi

    I have a service running on my website where users can light a candle it uses this code for fetching data from a database:

    PHP Code:
    //$Query = "SELECT ID, Name FROM candle ORDER BY ID ASC";

             
    $Query "SELECT ID, Name FROM candle WHERE ID > \"$preCount\" AND ID < \"$Count\" ORDER BY ID DESC";
             
    $DatabaseResult MySQL_query($Query$DatabaseLink);
             while(
    $DatabaseRow MySQL_fetch_object($DatabaseResult))
             {
                      
    $nume $DatabaseRow->Name;
                      print(
    "<td><img src=\"images/candles.gif\" width=\"35\" height=\"65\"><br>$nume</td>");
                      
    $interval++;
                      if(
    $interval == 1)
                      {
                               print(
    "</tr><tr>");
                               
    $interval 0;
                      }
             }
    ?> 
    When the info is selected from the database, it displays 100 candles in ascending order so the first ever candle is displayed and the next 99 after that.

    I would like for the above code to select the latest 100 candles, starting with the very latest one and showing the next 99 in descending order.

    I did try changing this code:

    PHP Code:
    $Query "SELECT ID, Name FROM candle WHERE ID > \"$preCount\" AND ID < \"$Count\" ORDER BY ID DESC"
    By it stops working completely.

    Can anyone help?

    Thanks

    Antony

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Try to remove this part of your query and see if it works
    WHERE ID > \"$preCount\" AND ID < \"$Count\"
    If you echo your $Query it will show you those values and should tell you why it's not working

  • #3
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    PHP Code:
     $Query "SELECT `ID`, `Name` FROM `candle` WHERE `ID` > '$preCount' AND `ID` < '$Count' ORDER BY `ID` DESC LIMIT 100"
    EDIT:

    The numerous backslashes aren't necessary mate . You can use " ' " as well.

    PHP Code:
    print("<td><img src='images/candles.gif' width='35' height='65' /><br />$nume</td>"); 
    Last edited by Deacon Frost; 01-07-2010 at 04:30 PM.

  • #4
    New Coder
    Join Date
    Oct 2008
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi

    Cheers, just one thing though, when I removed that bit of coding it displays the whole database.

    I just want it to display the last 50 posts, I know how to limit the number of posts shown via count/precount code here:

    PHP Code:
    <?
             
    print($TotalCount);
             if(!isset(
    $preCount))
             {
                      
    $preCount $TotalCount 10;
             }
             if(!isset(
    $Count))
             {
                      
    $Count $TotalCount 10;
             }

    ?>

    <?
    if($submit == "Search" || $yes == "yes")
    {
            print(
    "");
             print(
    "Search Result:  ");
             
    $Query "SELECT ID, Name FROM candle WHERE Name LIKE \"%$search%\" ORDER BY ID DESC";
             
    $DatabaseResult MySQL_query($Query$DatabaseLink);
             while(
    $DatabaseRow MySQL_fetch_object($DatabaseResult))
             {
            
    $id $DatabaseRow->ID 5;
               print(
    "<a href=\"index.php?Count=$id\">$DatabaseRow->Name</a>");
             }

             if(
    MySQL_num_rows($DatabaseResult) == 0)
             {
                      print(
    "None found.");
            print(
    "<a href=\"index.php?Count=$TotalCount\"></a>");
             }
    }else
    {

    ?>

    <table cellspacing="30"><tr align="left">
    <?

             
    if($Count 10)
             {
                      
    $preCount $Count 10;
             }
    But here I have selected 10, it then shows the 10 oldest posts rather then the 10 latest posts.

    Thanks

    Antony

  • #5
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    You dont have the LIMIT at the end of the query
    add LIMIT 50 to the end of it and it should work fine (You might want to change DESC to ASC if you're getting the wrong end of the posts)
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #6
    New Coder
    Join Date
    Oct 2008
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi

    How would that limit look in the query code? sorry but I am very new to coding

    Thank you

    Antony

  • #7
    New Coder
    Join Date
    Oct 2008
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi

    Solved it

    I just literally added limit 50 on the end

    Thanks again for the help

    Antony

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    lol yup that was what I meant
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

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