Jnerocorp Posted September 27, 2009 Share Posted September 27, 2009 ok i have a table called pins. in the table pins there are 2 rows id and pin I need to do something like this: SELECT highest id FROM pins i am just not sure how to select highest id Quote Link to comment https://forums.phpfreaks.com/topic/175722-solved-select-highest-id-number-from-rows/ Share on other sites More sharing options...
Zane Posted September 27, 2009 Share Posted September 27, 2009 SELECT MAX(id) as highest FROM pins Quote Link to comment https://forums.phpfreaks.com/topic/175722-solved-select-highest-id-number-from-rows/#findComment-926016 Share on other sites More sharing options...
Jnerocorp Posted September 27, 2009 Author Share Posted September 27, 2009 ok actually i made a mistake what i need is a code like this one SELECT pin FROM pins WHERE MAX(id) as highest I need to select the pin from table pins where the highest id is sorry for the confusion Quote Link to comment https://forums.phpfreaks.com/topic/175722-solved-select-highest-id-number-from-rows/#findComment-926018 Share on other sites More sharing options...
Zane Posted September 27, 2009 Share Posted September 27, 2009 SELECT pin FROM pins ORDER by id DESC Quote Link to comment https://forums.phpfreaks.com/topic/175722-solved-select-highest-id-number-from-rows/#findComment-926019 Share on other sites More sharing options...
Jnerocorp Posted September 27, 2009 Author Share Posted September 27, 2009 ok i tried this code $result = mysql_query("SELECT pin FROM pins ORDER by id DESC") or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo $row['pin']; echo "<br />"; } but it just displays every pin in descending order i need it to just display the one with the highest id not all of them Quote Link to comment https://forums.phpfreaks.com/topic/175722-solved-select-highest-id-number-from-rows/#findComment-926022 Share on other sites More sharing options...
Zane Posted September 27, 2009 Share Posted September 27, 2009 i need it to just display the one with the highest id not all of them then why would you loop through all of them? just echo it once. Quote Link to comment https://forums.phpfreaks.com/topic/175722-solved-select-highest-id-number-from-rows/#findComment-926042 Share on other sites More sharing options...
Jnerocorp Posted September 27, 2009 Author Share Posted September 27, 2009 how do i echo it only once? Quote Link to comment https://forums.phpfreaks.com/topic/175722-solved-select-highest-id-number-from-rows/#findComment-926045 Share on other sites More sharing options...
Zane Posted September 27, 2009 Share Posted September 27, 2009 $result = mysql_query("SELECT pin FROM pins ORDER by id DESC") or die(mysql_error()); // You echo it once by not using a loop in the first place //while($row = mysql_fetch_array($result)) // { echo $row['pin']; echo " "; // } Quote Link to comment https://forums.phpfreaks.com/topic/175722-solved-select-highest-id-number-from-rows/#findComment-926051 Share on other sites More sharing options...
Jnerocorp Posted September 27, 2009 Author Share Posted September 27, 2009 this is the code i am using it works perfectly $query = "SELECT pin, id FROM `pins` ORDER BY id DESC LIMIT 1"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_assoc($result); echo "The highest id is " . $row['id'] . " and has pin number: " . $row['pin']; what would be the code that i use to delete that id and pin out of the pins table? Quote Link to comment https://forums.phpfreaks.com/topic/175722-solved-select-highest-id-number-from-rows/#findComment-926052 Share on other sites More sharing options...
Zane Posted September 27, 2009 Share Posted September 27, 2009 $sql = "DELETE FROM pins WHERE id = {$row['id']}"; Quote Link to comment https://forums.phpfreaks.com/topic/175722-solved-select-highest-id-number-from-rows/#findComment-926054 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.