colap Posted June 17, 2015 Share Posted June 17, 2015 if (isset($_REQUEST['page']) ) { $offset = $_REQUEST['page']; } else { $offset = 0; } $limit = 2; $dbh = mysql_connection(); $sql = "select * from posts limit :limit offset :offset"; $stmt = $dbh->prepare($sql); $stmt->bindParam(':limit', $limit); $stmt->bindParam(':offset', $offset); $stmt->execute(); $result = $stmt->fetchAll(); It displays empty array(0) { } , what can be the reason? Link to comment https://forums.phpfreaks.com/topic/296872-cant-do-pagination-with-php-mysql-pdo/ Share on other sites More sharing options...
requinix Posted June 17, 2015 Share Posted June 17, 2015 Invalid SQL. SELECT Syntax Link to comment https://forums.phpfreaks.com/topic/296872-cant-do-pagination-with-php-mysql-pdo/#findComment-1514144 Share on other sites More sharing options...
colap Posted June 17, 2015 Author Share Posted June 17, 2015 Invalid SQL. SELECT Syntax Can't understand the syntax. What would be the correct syntax? http://stackoverflow.com/questions/5508993/pdo-limit-and-offset Link to comment https://forums.phpfreaks.com/topic/296872-cant-do-pagination-with-php-mysql-pdo/#findComment-1514149 Share on other sites More sharing options...
requinix Posted June 17, 2015 Share Posted June 17, 2015 Ah, I'm wrong. LIMIT...OFFSET is perfectly legal. Someone should have RTFMed. That thing you linked mentioned strings being bad. Try $offset = (int)$_REQUEST['page']; Link to comment https://forums.phpfreaks.com/topic/296872-cant-do-pagination-with-php-mysql-pdo/#findComment-1514150 Share on other sites More sharing options...
Barand Posted June 17, 2015 Share Posted June 17, 2015 Isn't the pdo default for binding parameters "string" type and you therefore need to specify PDO::PARAM_INT? Link to comment https://forums.phpfreaks.com/topic/296872-cant-do-pagination-with-php-mysql-pdo/#findComment-1514153 Share on other sites More sharing options...
Barand Posted June 17, 2015 Share Posted June 17, 2015 Ah, I'm wrong. LIMIT...OFFSET is perfectly legal. Someone should have RTFMed. I too had never noticed that OFFSET option in the LIMIT clause. Live 'n' learn. Link to comment https://forums.phpfreaks.com/topic/296872-cant-do-pagination-with-php-mysql-pdo/#findComment-1514156 Share on other sites More sharing options...
JenniferLawrence Posted June 17, 2015 Share Posted June 17, 2015 The mysql_connection() is bugging me. Are you mix matching the old deprecated mysql_* functions with PDO or is mysql_connection() going to be PDO's correct database connection? Link to comment https://forums.phpfreaks.com/topic/296872-cant-do-pagination-with-php-mysql-pdo/#findComment-1514196 Share on other sites More sharing options...
mac_gyver Posted June 17, 2015 Share Posted June 17, 2015 once you get past the query errors (see Barand's reply #5), you are using the page part of pagination incorrectly. the logical page number from the link - 1, 2, 3.... needs to be converted to the offset value for the query statement by subtracting one and multiplying by the rows-per-page/limit value. since the offset value is now a calculated value, it doesn't need to be a bound parameter in the query statement. likewise, if the limit value is defined in your php code, it doesn't need to be a bound parameter. Link to comment https://forums.phpfreaks.com/topic/296872-cant-do-pagination-with-php-mysql-pdo/#findComment-1514199 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.