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 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    87
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Exclamation What's wrong with this code

    What's wrong with the code below:

    Code:
    <?php 
     
          // Include database connection settings
          include('config.inc');  
      
          //first select the latest entry 
    
          $sql = mysql_query("SELECT * FROM entries order by Date DESC limit 1");  
    
          $result = mysql_query($sql) or die(mysql_error());   
     
        
            //display story data  
            while($row = mysql_fetch_assoc($result))  
            {  
                 
                echo ' ' . $row['Title'] .''; 
               
                  
            }  
    
          	   
            
          ?>

    I'm getting the following error message when I execute the code:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #10' at line 1

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Posts
    271
    Thanks
    3
    Thanked 40 Times in 40 Posts
    It looks like you are doing a query of a query.

    PHP Code:
    $sql mysql_query("SELECT * FROM entries order by Date DESC limit 1");  

    $result mysql_query($sql) or die(mysql_error());

    it should be 


     $sql 
    "SELECT * FROM entries order by Date DESC limit 1";  

     
    $result mysql_query($sql) or die(mysql_error()); 
    If you can't stand behind your troops, feel free to stand in front of them
    Semper Fidelis

  • #3
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    Quote Originally Posted by Thuita Maina View Post
    Code:
          $sql = mysql_query("SELECT * FROM entries order by Date DESC limit 1");  
    
          $result = mysql_query($sql) or die(mysql_error());
    It seems you do a query on the database and then you turn around and do a query on those results. I think what you meant to do was maybe something like...

    Code:
          $sql = "SELECT * FROM entries order by Date DESC limit 1";
    
          $result = mysql_query($sql) or die(mysql_error());

  • #4
    New Coder
    Join Date
    Sep 2012
    Posts
    87
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Thanks guys. It worked!


  •  

    Posting Permissions

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