butlimous Posted February 3, 2013 Share Posted February 3, 2013 Hey I have a simple articles table with IDs. I want to get the highest and lowest ids from the latest 10 results. For example, if there are 11 ids, the result should be 2 and 11 and if there are 4 ids, should be 4 and 1 and so on. I use PHP PDO statements. $aid = $DBH->prepare("SELECT id FROM articles ORDER BY id DESC LIMIT 10"); $aid->execute(); $row = $aid->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST); $lowest_article_id = $row[0]; $row = $aid->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_FIRST); $highest_article_id = $row[0]; The above will always return 11 if there are 11 records and 10 if there are 10 records. So, the query ignores the PDO::FETCH_ORI_LAST and PDO::FETCH_ORI_FIRST part of the query. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/273986-how-to-get-the-first-and-last-results-from-a-mysql-query-using-php-pdo/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 3, 2013 Share Posted February 3, 2013 Those two options are only available when the result set has been setup with a scrollable cursor (assuming your database type/driver supports scrollable cursors). You would need to add the following as the 2nd parameter to the ->prepare() statement - array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL) Quote Link to comment https://forums.phpfreaks.com/topic/273986-how-to-get-the-first-and-last-results-from-a-mysql-query-using-php-pdo/#findComment-1409877 Share on other sites More sharing options...
butlimous Posted February 3, 2013 Author Share Posted February 3, 2013 Hey Thanks for your input. Tried it like this before with no avail also. I don't know if it has something to do with configuration of PHP or what. $aid = $DBH->prepare("SELECT id FROM kisses ORDER BY id DESC LIMIT 10", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL)); $aid->execute(); $row = $aid->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST); $lowest_article_id = $row[0]; $row = $aid->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_FIRST); $highest_article_id = $row[0]; I can also get the correct results using the following query with subquery: SELECT MIN(id),MAX(id) FROM (SELECT id FROM articles ORDER BY id DESC LIMIT 10) t However a lot of people advise against using subqueries on a large scale because they are performance hit. Quote Link to comment https://forums.phpfreaks.com/topic/273986-how-to-get-the-first-and-last-results-from-a-mysql-query-using-php-pdo/#findComment-1409880 Share on other sites More sharing options...
Jessica Posted February 3, 2013 Share Posted February 3, 2013 What results DO you get when you use that code? Quote Link to comment https://forums.phpfreaks.com/topic/273986-how-to-get-the-first-and-last-results-from-a-mysql-query-using-php-pdo/#findComment-1409883 Share on other sites More sharing options...
butlimous Posted February 3, 2013 Author Share Posted February 3, 2013 What results DO you get when you use that code? Hey Jessica Which one? You mean the one after adding PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL ? I'm getting 11 and 10 (the reason for 10 is that using fetch another time will skip the previous results so the first time it fetched 11 and then at the second time it skips the 11th row and start from the 10th row) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/273986-how-to-get-the-first-and-last-results-from-a-mysql-query-using-php-pdo/#findComment-1409885 Share on other sites More sharing options...
PFMaBiSmAd Posted February 3, 2013 Share Posted February 3, 2013 (edited) https://bugs.php.net/bug.php?id=34625 ^^^ Assuming you are using a mysql database (lol, which I just reread your thread title and you are), this is not supported. You will need to use the fetchall() method and access the first and last element in the fetched array. Edited February 3, 2013 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/273986-how-to-get-the-first-and-last-results-from-a-mysql-query-using-php-pdo/#findComment-1409888 Share on other sites More sharing options...
butlimous Posted February 3, 2013 Author Share Posted February 3, 2013 Thanks....surprisingly very old bug! Quote Link to comment https://forums.phpfreaks.com/topic/273986-how-to-get-the-first-and-last-results-from-a-mysql-query-using-php-pdo/#findComment-1409901 Share on other sites More sharing options...
Jessica Posted February 3, 2013 Share Posted February 3, 2013 https://bugs.php.net/bug.php?id=34625 You will need to use the fetchall() method and access the first and last element in the fetched array. Just thinking out loud, but if all he wants is the first and last, wouldn't it be better to just do two queries, one ASC and one DESC? If OP is going to use the results at all that's different, but hypothetically if he only wants first and last? Quote Link to comment https://forums.phpfreaks.com/topic/273986-how-to-get-the-first-and-last-results-from-a-mysql-query-using-php-pdo/#findComment-1409903 Share on other sites More sharing options...
butlimous Posted February 4, 2013 Author Share Posted February 4, 2013 Hey Do you think would it be better performance wise using the following subquery: SELECT MIN(id),MAX(id) FROM (SELECT id FROM articles ORDER BY id DESC LIMIT 10) t Or using 2 separate queries?? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/273986-how-to-get-the-first-and-last-results-from-a-mysql-query-using-php-pdo/#findComment-1410031 Share on other sites More sharing options...
Barand Posted February 4, 2013 Share Posted February 4, 2013 I wouldn't worry about the performance hit with that subquery. When I ran these two queries on a 200,000 row table they both took less than 0.001 seconds. SELECT MIN(id),MAX(id) FROM (SELECT id FROM bigtable ORDER BY id DESC LIMIT 10) t; SELECT id FROM bigtable ORDER BY id DESC LIMIT 10; Quote Link to comment https://forums.phpfreaks.com/topic/273986-how-to-get-the-first-and-last-results-from-a-mysql-query-using-php-pdo/#findComment-1410062 Share on other sites More sharing options...
butlimous Posted February 5, 2013 Author Share Posted February 5, 2013 thanks Barand for your effort Quote Link to comment https://forums.phpfreaks.com/topic/273986-how-to-get-the-first-and-last-results-from-a-mysql-query-using-php-pdo/#findComment-1410153 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.