Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-07-01
7
Medium Priority
?
286 Views
Last Modified: 2012-05-07
how to check the maximum id in the database. what is the query for that?
0
Comment
Question by:designersx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:szewkam
ID: 24752637
The latest id in db? "SELECT id FROM table ORDER BY id DESC LIMIT 1" or "SELECT MAX(id) FROM table"
0
 

Author Comment

by:designersx
ID: 24752812
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
 

Author Comment

by:designersx
ID: 24752824
i am not able to get the latest id from the database, please look at the code i am using.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Accepted Solution

by:
szewkam earned 500 total points
ID: 24752871
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
 

Author Comment

by:designersx
ID: 24752912
it is exactly right , sir can we use mysql_insert_id(). can u please tell the usage of that.
0
 
LVL 7

Expert Comment

by:szewkam
ID: 24752960
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
 

Author Comment

by:designersx
ID: 24753023
thanks a lot.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question