LIMIT clause problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whizzkid1892
    New Member
    • Dec 2007
    • 5

    LIMIT clause problem

    Hi, I have a movie database set-up and would like to create coding to show each movie in the database on its own page, and give the user links in a "page 1, page 2...." type navgation system. I have attempted this with the coding below but when I run the script in my browser I get the following error "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 ' 1' at line 4".

    Code.

    Code:
    <?php
    $connect = mysql_connect
    ("host", "username", "password") or
    		die ("Hey loser, check your server connection!");
    
    mysql_select_db ("database");
    
    $offset=$_REQUEST['offset'];
    $query="SELECT movie_name, movie_year
    		FROM movie
    		ORDER BY movie_name
    		LIMIT $offset, 1";
    $results=mysql_query($query)
    		or die(mysql_error());
    echo	"<table>\n";[/CODE
    while	($rows=mysql_fetch_assoc($results))	{
    echo	"<tr>\n";
    	foreach($rows as $value)	{
    	echo "<td>\n";
    	echo	$value;
    	echo	"</td>\n";
    	}
    echo	"</tr><br>\n";
    }
    echo	"</table>\n";
    echo	"<a href='page.php?offset=0'>Page 1</a><br>";
    echo	"<a href='page.php?offset=1'>Page 2</a><br>";
    echo	"<a href='page.php?offset=2'>Page 3</a><br>";
    ?>
    Any help would be greatly appreciated.
  • harsh789
    New Member
    • Jun 2008
    • 1

    #2
    It seems when u load the page for the first time, the variable REQUEST['offset'] not set. This resulting your query looks like ... "LIMIT , 1" and hence giving sql error. In this case first check the value of $offset varible and if it is empty assing value 0. for example..

    [PHP]if($offset=="") $offset=0;[/PHP]

    Comment

    • whizzkid1892
      New Member
      • Dec 2007
      • 5

      #3
      Thanks but where would I put that new line of code into my existing code?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by whizzkid1892
        Thanks but where would I put that new line of code into my existing code?
        That's strange. Surely if you wrote that code yourself then you must know where to initialize your offset variable.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by whizzkid1892
          Thanks but where would I put that new line of code into my existing code?
          How about somewhere between the line where you fetch the offset value and the line where you use it?

          I would have thought that would be pretty obvious :P

          Comment

          Working...