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

how to check the maximum id in the database. what is the query for that?
designersxAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
szewkamConnect With a Mentor Commented:
mysql_query returns type resource not data. If you want to display max use code from snippet:
<?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);
        list($id) = mysql_fetch_row($query);
        echo $id;
?>

Open in new window

0
 
szewkamCommented:
The latest id in db? "SELECT id FROM table ORDER BY id DESC LIMIT 1" or "SELECT MAX(id) FROM table"
0
 
designersxAuthor 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

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
designersxAuthor Commented:
i am not able to get the latest id from the database, please look at the code i am using.
0
 
designersxAuthor Commented:
it is exactly right , sir can we use mysql_insert_id(). can u please tell the usage of that.
0
 
szewkamCommented:
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
0
 
designersxAuthor Commented:
thanks a lot.
0
All Courses

From novice to tech pro — start learning today.