gaffafoote Posted April 27, 2007 Share Posted April 27, 2007 Hi All, Relatively inexperienced php'er. I can't see anything wrong with this code at all. Trying to retreive the last(highest) id from the text_id column which is auto-increment and primary key. The conncetion to the DB is fine and working. It appears that the result isn't being drawn though........any ideas?? <?php require_once ('mysql_connect.php'); //Connect to DB $query = "SELECT MAX (text_id) FROM text"; $result = mysql_query ($query); //run the query echo $result; $row = @mysql_fetch_row($result); $tid = $row[0]; echo $tid; mysql_close(); ?> Cheers, Gareth. Link to comment https://forums.phpfreaks.com/topic/49012-solved-this-should-be-so-very-simplei-think-retrieving-max-numeric-value/ Share on other sites More sharing options...
sanfly Posted April 27, 2007 Share Posted April 27, 2007 Hmmm, I dont really know anything about the MAX syntax, so here is how I would do it $query = "SELECT text_id FROM text ORDER BY text_id DESC LIMIT 1"; Link to comment https://forums.phpfreaks.com/topic/49012-solved-this-should-be-so-very-simplei-think-retrieving-max-numeric-value/#findComment-240079 Share on other sites More sharing options...
per1os Posted April 27, 2007 Share Posted April 27, 2007 For one if it does not work why are you supressing errors? Not a very good idea. Second it is probably in your query statement, add the OR DIE(mysql_error()) to see. <?php require_once ('mysql_connect.php'); //Connect to DB $query = "SELECT MAX (text_id) FROM text"; $result = mysql_query ($query) OR DIE(mysql_error()); //run the query echo $result; $row = mysql_fetch_row($result); // remove the error supressor $tid = $row[0]; echo $tid; mysql_close(); ?> Error messages can be very useful sometimes. Link to comment https://forums.phpfreaks.com/topic/49012-solved-this-should-be-so-very-simplei-think-retrieving-max-numeric-value/#findComment-240080 Share on other sites More sharing options...
gaffafoote Posted April 27, 2007 Author Share Posted April 27, 2007 Thanks to both of you. I'm already falling into bad habits, must work on that. It seems that the problem is with th SQL syntax. If anybody who knows where i'm going wrong with this, i'd appreciate the help because this syntax has been taken directly from a couple of online sources. Cheers. Link to comment https://forums.phpfreaks.com/topic/49012-solved-this-should-be-so-very-simplei-think-retrieving-max-numeric-value/#findComment-240092 Share on other sites More sharing options...
per1os Posted April 27, 2007 Share Posted April 27, 2007 It may have to do with your MySQL version. Post waht the SQL error message display, that would help you out tremondously, not to mention the people who want to help you out. Link to comment https://forums.phpfreaks.com/topic/49012-solved-this-should-be-so-very-simplei-think-retrieving-max-numeric-value/#findComment-240095 Share on other sites More sharing options...
gaffafoote Posted April 27, 2007 Author Share Posted April 27, 2007 This is the SQL error from the first post. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(text_id) FROM text' at line 1 After SANFLY's suggestion I have tried this code and got the message Resource ID#380: $query = "SELECT text_id FROM text ORDER BY text_id DESC LIMIT 1"; $result = mysql_query ($query) OR DIE(mysql_error()); echo $result; $tid = mysql_result($result, 0); cheers. Link to comment https://forums.phpfreaks.com/topic/49012-solved-this-should-be-so-very-simplei-think-retrieving-max-numeric-value/#findComment-240112 Share on other sites More sharing options...
sanfly Posted April 27, 2007 Share Posted April 27, 2007 So is there still a problem? Try <? $query = "SELECT text_id FROM text ORDER BY text_id DESC LIMIT 1"; $result = mysql_query ($query) OR DIE(mysql_error()); $row = mysql_fetch_array($result); echo $row['tid']; ?> Link to comment https://forums.phpfreaks.com/topic/49012-solved-this-should-be-so-very-simplei-think-retrieving-max-numeric-value/#findComment-240119 Share on other sites More sharing options...
per1os Posted April 27, 2007 Share Posted April 27, 2007 Chances are it is the space between MAX and (text_id) Try it with it like this MAX(text_id) see if that works. Link to comment https://forums.phpfreaks.com/topic/49012-solved-this-should-be-so-very-simplei-think-retrieving-max-numeric-value/#findComment-240120 Share on other sites More sharing options...
gaffafoote Posted April 27, 2007 Author Share Posted April 27, 2007 Just realised I was being a dumbass and both of these ways were working. It was the space after the MAX which was causing a problem and I don't know why SPANFLY's method didn't work first time but they're both working now. Peas and Love. $query = "SELECT text_id FROM text ORDER BY text_id DESC LIMIT 1"; $result = mysql_query ($query) OR DIE(mysql_error()); $row = mysql_fetch_array($result); echo $row[0]; /***************************/ $query = "SELECT MAX(text_id) FROM text"; $result = mysql_query ($query) OR DIE(mysql_error()); //run the query $row = mysql_fetch_row($result); echo $row[0]; Link to comment https://forums.phpfreaks.com/topic/49012-solved-this-should-be-so-very-simplei-think-retrieving-max-numeric-value/#findComment-240131 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.