Jump to content


Photo

how do i add to a ID number


  • Please log in to reply
2 replies to this topic

#1 dharm

dharm
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 17 June 2006 - 02:15 PM

Hi I have this script that calls the most recent ID number from mysql using last_insert_id() but I would like to add 1 to make a new ID. For some reason when I try to add 1 to my six digit ID it returns less numbers.. for example ID is 000123. if I add 1 to 000123 I get 124. Im not sure if im using the right method.. Here is the code, any help would be much appreciated.

$query1 = "Select *, last_insert_id() from ".$DBprefix."client_info ORDER BY `id` DESC LIMIT 1"; 
$result = mysql_query($query1);
if (mysql_errno())
{
die("<br>" . mysql_errno() .": ".mysql_error()."<br>");
}
$row = mysql_fetch_row($result);
 if (mysql_errno())
{
die("<br>" . mysql_errno() .": ".mysql_error()."<br>");
}
$newid = $row[0];
echo "<br>Assigned ID: $newid"; // this echos 000123

$newid++;

echo "$newid";  // this echos 124

Thx.


#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 17 June 2006 - 02:27 PM

echo sprintf("%06d", $newid);  // this echos 000124

[a href=\"http://www.php.net/sprintf\" target=\"_blank\"]http://www.php.net/sprintf[/a]
~ D Kuang

#3 dharm

dharm
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 17 June 2006 - 04:03 PM

thank you poirot!... Does anyone know how i could also check for the ID code within another table aswell as client_info.. whas the best way to structure this? thx. [quote]thank you!... Does anyone know how i could also check for the ID code within another table aswell as client_info.. whas the best way to structure this? thx.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users