Current location: Hot Scripts Forums » Programming Languages » PHP » How to read the last row of Db in SQl +PHP


How to read the last row of Db in SQl +PHP

Reply
  #1 (permalink)  
Old 03-10-08, 05:43 AM
csv's Avatar
csv csv is offline
Newbie Coder
 
Join Date: Feb 2008
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Trophee How to read the last row of Db in SQl +PHP

Hi all ,

i want to generate ID for asset that is based on input in different fields. When the asset is first time added to data base..
its ID is defined as...
PHP Code:

$MY_ID="const"."/".$month."/".$year."/".$class_of_asset."/"."number"
its o/p should look like this


MY_ID=aaa/02/08/PSB/1
MY_ID=aaa/02/08/SE/2
MY_ID=aaa/02/08/CP/3


as u can see in the $MY_ID o/p "number" every times increment
by one. so somebody give me the idea to remember the number is to
read the last entry in the asset table n read its MY_ID and separate the
last part of string which is number .so that u can increment it by one for
making a ID for asset


bt the problem is how to read the last row of database
ny command or query to slove this problem..
or Any other logic to solve this problem..


Thanks in Advance
$V
Reply With Quote
  #2 (permalink)  
Old 03-10-08, 06:11 AM
lnunesbr lnunesbr is offline
Newbie Coder
 
Join Date: Mar 2008
Location: São Paulo
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
simple man....

for example:

PHP Code:

//insert a record in data base
$qry mysql_query("insert into db a record");

//now we get the last id inserted in db, be sure to be exactly after the insert query
$last_id mysql_insert_id();

//defining $MY_ID variable
//$MY_ID="const"."/".$month."/".$year."/".$class_of_asset."/"."number";
$MY_ID="const"."/".$month."/".$year."/".$class_of_asset."/"$last_id
That's it!
Reply With Quote
  #3 (permalink)  
Old 03-11-08, 12:58 AM
csv's Avatar
csv csv is offline
Newbie Coder
 
Join Date: Feb 2008
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for d reply bt the problem is the command tat u hav suggested is used for auto increment operation
PHP Code:

 int mysql_insert_id  ([ resource $link_identifier  ] )


Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
bt i hav not used any auto increment in my DB?

So please suggest the other way for reading the last row of table
Reply With Quote
  #4 (permalink)  
Old 03-11-08, 06:09 AM
lnunesbr lnunesbr is offline
Newbie Coder
 
Join Date: Mar 2008
Location: São Paulo
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
the best way is defining an auto increment field...

by the way, you can do something like this

PHP Code:

   //insert a record in data base
$qry mysql_query("insert into table ");

//now we get the last id inserted in db, be sure to be exactly after the insert query
$qry_get_number mysql_query("select max(number) from table");
list(
$last_id) = mysql_fetch_array($qry_get_number);

//defining $MY_ID variable
$MY_ID="const"."/".$month."/".$year."/".$class_of_asset."/"$last_id
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and SQL versions. I'm not up to date, but is it okay? primalminds Web Servers 2 05-14-07 07:34 PM
How to read the URL in the address bar with PHP bshipman PHP 2 05-07-07 03:05 PM
HELP - Execute sql file with php script alex11 PHP 4 04-29-07 02:29 AM
php and sql website help therunners Job Offers & Assistance 2 06-16-05 08:43 PM
Need PHP & SQL Script- Willing to Pay! dayzeday Script Requests 4 11-12-03 06:35 PM


All times are GMT -5. The time now is 07:38 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.