Gem Posted March 12, 2009 Share Posted March 12, 2009 Hi How do I limit the results returned by php from my database?? I.e. I have a table of articles, and I want to display the newest 5 articles on my homepage... How do I do that? This is the code I have so far... but this displays every record ... <?php $con = mysql_connect("CONNECTION DETAILS"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("bssql", $con);$result = mysql_query("SELECT * FROM articles ORDER BY ID DESC");while($row = mysql_fetch_array($result)) { echo $row['Title']; echo $row['SubTitle']; echo "<br />"; }mysql_close($con); ?> Thanks in advance .. Gem PS: Can you tell me exactly where I need to add something if necessary ... I always mess things up otherwise Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/ Share on other sites More sharing options...
lonewolf217 Posted March 12, 2009 Share Posted March 12, 2009 something along the lines of select * from ( select *,ROW_NUMBER() OVER (ORDER BY id desc) as row from articles ) a where row <=5" Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782581 Share on other sites More sharing options...
Gem Posted March 12, 2009 Author Share Posted March 12, 2009 Would: SELECT * FROM articles ORDER BY ID DESC LIMIT 5 Work?? Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782583 Share on other sites More sharing options...
lonewolf217 Posted March 12, 2009 Share Posted March 12, 2009 not everything supports LIMIT, i forget what the "limitation" is (pun intended) but it never worked for me on MYSQL Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782586 Share on other sites More sharing options...
samshel Posted March 12, 2009 Share Posted March 12, 2009 strange...LIMIT n1,n2 always worked for me.. may be u can try it and post what it does ? Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782594 Share on other sites More sharing options...
Gem Posted March 12, 2009 Author Share Posted March 12, 2009 <?php $con = mysql_connect("CONNECTION"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("bssql", $con);$result = mysql_query("SELECT title, subtitle FROM articles ORDER BY ID DESC LIMIT 2");while($row = mysql_fetch_array($result)) { echo $row['Title']; echo $row['SubTitle']; }mysql_close($con); ?> Had to do 2 as theres only 3 records in the table lol ... doesnt do anything though ... all i get is a blank page ... I think I messed up the code ... the query works in SQLyog ...? Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782604 Share on other sites More sharing options...
samshel Posted March 12, 2009 Share Posted March 12, 2009 <?php $con = mysql_connect("CONNECTION"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("bssql", $con); $result = mysql_query("SELECT title, subtitle FROM articles ORDER BY ID DESC LIMIT 2") or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo $row['Title']; echo $row['SubTitle']; } mysql_close($con); ?> try this.. Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782606 Share on other sites More sharing options...
Gem Posted March 12, 2009 Author Share Posted March 12, 2009 Hmmm ... still nothing?! ??? Did I do something to the Echo part?? no errors is weird... Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782610 Share on other sites More sharing options...
redarrow Posted March 12, 2009 Share Posted March 12, 2009 got to be a database set up problem the code provided is beautiful nice coding op. Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782612 Share on other sites More sharing options...
samshel Posted March 12, 2009 Share Posted March 12, 2009 try mysql_fetch_assoc() instead of mysql_fetch_array() Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782614 Share on other sites More sharing options...
redarrow Posted March 12, 2009 Share Posted March 12, 2009 all i can add like i said it already perfect. <?php $con = mysql_connect("CONNECTION"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("bssql", $con); $result = mysql_query("SELECT `title`, `subtitle` FROM `articles` ORDER BY `ID` DESC LIMIT `2`") or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { echo $row['Title']; echo $row['SubTitle']; } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782617 Share on other sites More sharing options...
Gem Posted March 12, 2009 Author Share Posted March 12, 2009 red I copied your code and it didnt like the LIMIT '2' , came back with an error ... so I did your code with LIMIT 2 .. and I've got a blank again ... this is interesting though, without the LIMIT 2 bit, the original code works fine ... with the LIMIT 2 I get no errors, but also no content .... Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782627 Share on other sites More sharing options...
Philip Posted March 12, 2009 Share Posted March 12, 2009 while($row = mysql_fetch_assoc($result)) { echo $row['Title']; echo $row['SubTitle']; } you're calling for Title, when it should be title - same with subtitle Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782640 Share on other sites More sharing options...
lonewolf217 Posted March 12, 2009 Share Posted March 12, 2009 red I copied your code and it didnt like the LIMIT '2' , came back with an error ... so I did your code with LIMIT 2 .. and I've got a blank again ... this is interesting though, without the LIMIT 2 bit, the original code works fine ... with the LIMIT 2 I get no errors, but also no content .... what error ? Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782656 Share on other sites More sharing options...
redarrow Posted March 12, 2009 Share Posted March 12, 2009 try this and cheek your spelling like uppercase letters are not the same as lowercase letters in php. currently ID << capitalized is it in the database cheek. <?php $con = mysql_connect("CONNECTION"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("bssql", $con); $result = mysql_query("SELECT `title`, `subtitle` FROM `articles` ORDER BY `ID` DESC LIMIT 2") or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { echo $row['title']; echo $row['subTitle']; } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782662 Share on other sites More sharing options...
Philip Posted March 12, 2009 Share Posted March 12, 2009 red I copied your code and it didnt like the LIMIT '2' , came back with an error ... so I did your code with LIMIT 2 .. and I've got a blank again ... this is interesting though, without the LIMIT 2 bit, the original code works fine ... with the LIMIT 2 I get no errors, but also no content .... what error ? Caused by the ticks around the 2 in the limit clause. They shouldnt be there Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782670 Share on other sites More sharing options...
Gem Posted March 12, 2009 Author Share Posted March 12, 2009 Thanks guys ... we've cracked it ... it was just a caps/lower case problem ... Thanks again x Quote Link to comment https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/#findComment-782862 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.