We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

how to check the maximum id in the database. what is the query for that?

designersx
designersx asked
on
Medium Priority
303 Views
Last Modified: 2012-05-07
how to check the maximum id in the database. what is the query for that?
Comment
Watch Question

Commented:
The latest id in db? "SELECT id FROM table ORDER BY id DESC LIMIT 1" or "SELECT MAX(id) FROM table"

Author

Commented:
the output is Resource id #3
<?php
	$db_host="localhost";
	$db_name="modified_cms";
	$username="root";
	$password="";
	$db_con=mysql_connect($db_host,$username,$password) or die("connection not build".mysql_error());
	$db=mysql_select_db($db_name) or die("database not build".mysql_error());
	
	$sql = "select max(id) from products";
	$query = mysql_query($sql);
	echo $query;
?>

Open in new window

Author

Commented:
i am not able to get the latest id from the database, please look at the code i am using.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
it is exactly right , sir can we use mysql_insert_id(). can u please tell the usage of that.

Commented:
well you can use mysql_insert_id if you want to retrieve id right after executing the query. In other cases if you using queries to add some data to other tables you can't be sure that this function gave you the expected value.
Usage is given on: http://pl2.php.net/manual/en/function.mysql-insert-id.php

Author

Commented:
thanks a lot.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.