Solved

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

Posted on 2009-07-01
7
281 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
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:szewkam
Comment Utility
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
Comment Utility
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
Comment Utility
i am not able to get the latest id from the database, please look at the code i am using.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Accepted Solution

by:
szewkam earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks a lot.
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now