jagularen Posted November 29, 2011 Share Posted November 29, 2011 Hi, This line of code works just fine for me: $sql_quest = 'SELECT id, username FROM user ORDER BY id ASC LIMIT 0, 30'; I want to be able to set the integer values dynamically and I thought something like this would work: $sql_quest = 'SELECT id, username FROM user ORDER BY id ASC LIMIT' . $my_int_value . ',' . $my_int_value + 30; But it doesn't... Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/252081-how-to-set-the-limit-dynamically/ Share on other sites More sharing options...
Spring Posted November 29, 2011 Share Posted November 29, 2011 $sql_quest = 'SELECT id, username FROM user ORDER BY id ASC LIMIT '. $my_int_value . ',' . $my_int_value + 30'; How's this? Are you getting an error? Can 'ya post it? Quote Link to comment https://forums.phpfreaks.com/topic/252081-how-to-set-the-limit-dynamically/#findComment-1292423 Share on other sites More sharing options...
jagularen Posted November 29, 2011 Author Share Posted November 29, 2011 Wow! That was quick! Thanks but this results in a syntax error... Quote Link to comment https://forums.phpfreaks.com/topic/252081-how-to-set-the-limit-dynamically/#findComment-1292425 Share on other sites More sharing options...
Spring Posted November 30, 2011 Share Posted November 30, 2011 What's the problem though? What error are you getting with yours? Quote Link to comment https://forums.phpfreaks.com/topic/252081-how-to-set-the-limit-dynamically/#findComment-1292426 Share on other sites More sharing options...
jagularen Posted November 30, 2011 Author Share Posted November 30, 2011 When I use: $sql = $this->db->prepare($sql_quest); $sql->execute(); I get: Fatal error: Call to a member function execute() on a non-object Quote Link to comment https://forums.phpfreaks.com/topic/252081-how-to-set-the-limit-dynamically/#findComment-1292428 Share on other sites More sharing options...
Spring Posted November 30, 2011 Share Posted November 30, 2011 When I use: $sql = $this->db->prepare($sql_quest); $sql->execute(); I get: Fatal error: Call to a member function execute() on a non-object Mind posting the whole code? Quote Link to comment https://forums.phpfreaks.com/topic/252081-how-to-set-the-limit-dynamically/#findComment-1292446 Share on other sites More sharing options...
PFMaBiSmAd Posted November 30, 2011 Share Posted November 30, 2011 Your original issue is that you need some white-space after the LIMIT keyword. The additional single-quote that Spring added on the end resulted in an odd number of quotes, which would be a php syntax error. Also, the second number in the LIMIT clause is not the ending row number, it is the number of rows, so, it will remain 30 (unless you want to change the number of rows.) Only the the first number, the starting row number, changes. It's usually less error prone to use overall double-quotes when building a query. You can then put php variables directly inside the query string - $sql_quest = "SELECT id, username FROM user ORDER BY id ASC LIMIT $my_int_value,30"; Quote Link to comment https://forums.phpfreaks.com/topic/252081-how-to-set-the-limit-dynamically/#findComment-1292449 Share on other sites More sharing options...
jagularen Posted November 30, 2011 Author Share Posted November 30, 2011 Thanks guys! I forgot the brackets at the end... $sql_quest = 'SELECT id, username FROM user ORDER BY id ASC LIMIT' . $my_int_value . ',' . ($my_int_value + 30); I'm new here and when I solved this yesterday I was trying to reply to my own last reply to say that I solved it or mark the post as solved. How can I do this? Quote Link to comment https://forums.phpfreaks.com/topic/252081-how-to-set-the-limit-dynamically/#findComment-1292822 Share on other sites More sharing options...
requinix Posted November 30, 2011 Share Posted November 30, 2011 or mark the post as solved. There's a button at the bottom of the thread after the posts. Quote Link to comment https://forums.phpfreaks.com/topic/252081-how-to-set-the-limit-dynamically/#findComment-1292826 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.