DIY-Forum Posted March 4, 2015 Share Posted March 4, 2015 Hello again my friends I've had some problems again, I'm trying to select the last id, or the max(id) but it keeps returning resource id. <?php $getLastData = mysql_query("SELECT MAX(id) FROM `database`.`forumTopics` WHERE category='$outData->id'");$getInfo = mysql_fetch_object(mysql_query("SELECT * FROM `database`.`forumTopics` WHERE id='$getLastData'")) or die("Error: ".mysql_error());?> Hope anyone can help Thanks, Kris Link to comment Share on other sites More sharing options...
justsomeguy Posted March 4, 2015 Share Posted March 4, 2015 mysql_query returns a MySQL result resource, not a value from a table. You need to use the other mysql functions to get the data from the result. Ideally, considering the fact that the PHP mysql extension has been out of date and replaced for over a decade, you would be using mysqli or PDO instead. Link to comment Share on other sites More sharing options...
DIY-Forum Posted March 4, 2015 Author Share Posted March 4, 2015 fetch will not work.. tried assoc too, but that don't work, so I have no idea on how to get the value of Max(id). left it blank for someone to fill out. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 4, 2015 Share Posted March 4, 2015 You can use fetch_array and then access the first element in the array, or you can give the max an alias and then use fetch_assoc or fetch_object. Link to comment Share on other sites More sharing options...
DIY-Forum Posted March 4, 2015 Author Share Posted March 4, 2015 You can use fetch_array and then access the first element in the array, or you can give the max an alias and then use fetch_assoc or fetch_object. Ok, I tried to do an fetch_assoc, but is the right way to do that by requesting it like from an array? in that case, I could not get it to work SELECT MAX(id) as maxId FROM table WHERE category='0';$likethis = $assocquery['maxId']; Link to comment Share on other sites More sharing options...
justsomeguy Posted March 4, 2015 Share Posted March 4, 2015 The manual shows an example about how to use it, but notice the red box on the top of the page.http://php.net/manual/en/function.mysql-fetch-assoc.phpYou can also use print_r on the array to see everything that's in it. Link to comment Share on other sites More sharing options...
DIY-Forum Posted March 4, 2015 Author Share Posted March 4, 2015 Thanks I managed to get it right, $result=mysql_query("SELECT max(id) as total from `database`.`forumTopics` WHERE category='$outData->id'");$data=mysql_fetch_assoc($result);echo $data['total']; And about upgrading to mysqli, I will do that, but I just learned about that when posting here last week, and yes indeed it has gone over a decade since I learned to code, so guess it's true what you say there! hehe.. I will update my scripts with time, thanks for letting me know though Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now