Jump to content

Get the last id of a table?


DIY-Forum

Recommended Posts

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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...