completeNewb Posted October 31, 2011 Share Posted October 31, 2011 Hi, I'm trying to get the last number from a column of numbers, add 1 to it and use this to insert a new value in the column. The column is not the primary key, indexed, or auto insert, it's just a plain old int field. $newid = mysql_query('SELECT IDCOL FROM prods ORDER BY IDCOL DESC LIMIT 1'); // the above just doesn't work--it returns 0, there are over 1200 products--I'm going nuts over this. //$newid += 1; I've commented this out, it works though (in that it adds 1 to 0) $sql = mysql_query("INSERT INTO prods (IDCOL , Prod_Code, blah... blah..) VALUES('$newid','$prod_code','blah... blah...')") or die (mysql_error()); //this works (but inserts 0 as the newID) You'd think this would be so easy. Thanks Steve Quote Link to comment https://forums.phpfreaks.com/topic/250135-get-last-row-in-a-column/ Share on other sites More sharing options...
silkfire Posted October 31, 2011 Share Posted October 31, 2011 If you have auto increment on your ID column then you don't need this meaningless procedure... Quote Link to comment https://forums.phpfreaks.com/topic/250135-get-last-row-in-a-column/#findComment-1283550 Share on other sites More sharing options...
seany123 Posted October 31, 2011 Share Posted October 31, 2011 You should really use the Search function on this site, i found about 10 threads asking the same question... assuming your using ID and with an auto increment then this should do it. this was just c+p from another thread... $sql = "SELECT * FROM prods ORDER BY IDCOL DESC limit 0,1" ; if that doesnt work... you could use a fetch_num_rows to get the latest id (assuming you dont delete any rows).. then use the number of rows against the IDCOL Quote Link to comment https://forums.phpfreaks.com/topic/250135-get-last-row-in-a-column/#findComment-1283553 Share on other sites More sharing options...
completeNewb Posted October 31, 2011 Author Share Posted October 31, 2011 Thanks for your replies... first, auto increment is not an option, unfortunately I have two databases to juggle, one is a subset of the other (products on the website). The master database is ms sql which won't run on a unix server (the website). The master database is on an office server with a poor connection unsuited for web users to connect to. second, fetch_num_rows to get the latest id (assuming you dont delete any rows)-- but, hey, rows will get deleted. So, I need a web database that will accept updated IDs from the master database. I searched long and hard and tried numerous solutions. Sorry but the my meaningless procedure is the best I could come up with. Believe me, i would be overjoyed if anyone's got another. $sql = "SELECT * FROM prods ORDER BY IDCOL DESC limit 0,1" ; looks to me like it would select every column (?) when i only want the last ID, but I just tried it and sadly it also returns 0. This sql: SELECT IDCOL FROM prods ORDER BY IDCOL DESC LIMIT 1 works in phpMyAdmin, but once i write it as php... $newid = mysql_query('SELECT IDCOL FROM prods ORDER BY IDCOL DESC LIMIT 1'); it returns 0. I don't understand why. Steve Quote Link to comment https://forums.phpfreaks.com/topic/250135-get-last-row-in-a-column/#findComment-1283559 Share on other sites More sharing options...
kicken Posted October 31, 2011 Share Posted October 31, 2011 mysql_query returns a result resource, not the actual result. To get the field value you need to call mysql_fetch_array() and then pull the column out of that array. $res = mysql_query('SELECT IDCOL FROM prods ORDER BY IDCOL DESC LIMIT 1'); $row = mysql_fetch_array($res); $newid = $row['IDCOL']; Quote Link to comment https://forums.phpfreaks.com/topic/250135-get-last-row-in-a-column/#findComment-1283600 Share on other sites More sharing options...
completeNewb Posted October 31, 2011 Author Share Posted October 31, 2011 kicken, not only did you explain why my efforts failed but your solution works like a charm! endless thanks, Steve Quote Link to comment https://forums.phpfreaks.com/topic/250135-get-last-row-in-a-column/#findComment-1283601 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.