Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Incrementing a number with php 1

Status
Not open for further replies.

btween

Programmer
Joined
Aug 7, 2003
Messages
338
Location
US
I am trying to increment an account number with php but I think I am having a data conversion error. When I add 1 to the recordset value I get a weird string rather than the next number.

Please help. Thanks

here is the code:

<?php echo $row_Recordset1['accountnumber']; ?>
<?php $account = $row_Recordset1['accountnumber'] + 1;
echo $account;
?>

this is what I get:7601026500010001 7.60102650001E+15

this is what I am supposedto get 76010265000100017 7601026500010002
 
before adding did u try an echo???

Known is handfull, Unknown is worldfull
 
It's almost certainly a problem with your trying to represent numbers that are too large.

PHP's integers are 32-bit signed numbers. (PHP does not support unsigned integers). The number you're trying to increment is larger than the biggest integer PHP can handle, so PHP converts it to a float.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
There is a workaround using PHP's bcmath functions.

I created a MySQL table (I assume from your variablename "$row_Recordset1" that this data is coming from a database) called foo as:

create table foo (thenum bigint unsigned);

and populated it with:
insert into foo (thenum) values (7601026500010001);

My script fetches the value, displays it, and increments it correctly using bcadd():

Code:
<?php
mysql_connect ('localhost', 'test', 'test');
mysql_select_db('test');

$rh = mysql_query ("SELECT * from foo");

list($a) = mysql_fetch_array($rh);

print $a;
print '<br>';
print bcadd($a, 1);
?>

I get as a return:

7601026500010001
7601026500010002





Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Ok This is what I have now. But instead of printing the account number it prints the autonumber field id . How can I select the column titled accountnumber?

thanks

<?php require_once('Connections/myconn.php'); ?>
<?php
mysql_select_db($database_myconn, $myconn);
$query_Recordset1 = "SELECT * FROM member ORDER BY id DESC";
$Recordset1 = mysql_query($query_Recordset1, $myconn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

$totalRows_Recordset1 = mysql_num_rows($Recordset1);

list($row_Recordset1['accountnumber']) = mysql_fetch_array($Recordset1);

print bcadd($row_Recordset1['accountnumber'], 1);
echo ($row_Recordset1['accountnumber']);
?>


 
I'm a bit confused by your posted code. I see you issuing a query once, but running mysql_fetch_* functions on it once.

And I do not understand at all what it is you're trying to do with this line at all:

list($row_Recordset1['accountnumber']) = mysql_fetch_array($Recordset1);


What is it you're trying to do?



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Basically when a new user registers I have to take the last account number add one to it and insert it along with the rest information in the datbase. The way I thought of doing this is creating a recordset, sorting by id descending, and the first row would have the last account number. This is how the algorithm kknows how to find the value that needs to be incremented.

The first field in the database is id which is an autonumber. Since the db can have only one autonumber this is what I'm trying to do to increment a field called accountnumberr which is set to long int.

thanks for your help
 
Keep in mind that you're writing a multiuser application, and concurrency can be a problem.

If your code fetches the last account number and increments it, it is possible that two users' using the script at once will interfere with each other and both will get the same ID.

Why not just use the autonumber column for your account numbers?



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I can't do this because the whole database structure needs to be changed and I can't do that.

I could do this very easily with vbscript and asp like this:
<% accountnumber = rsNewMember("accountnumber") + 1 %>

and then when I do the insert trecord I just enter accountnumber in the db.

how can i do this with php?

thx
 
Insufficient data for a meaningful answer. Assume I know little about VBScript/ASP and nothing about what rsNewMember is.

But even with VBScript/ASP, if new IDs are being created in program code, you can run into the ID collision problem I told you about earlier.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
RsNewMember is the name of the recordset. It just sais:

accountnumber is equal to field "accountnumber" of recordset RsNewMember + 1

and then I just do an insert record inserting the variable accountnumber.

This should work ok unless you have a better idea of how this can be done without altering the db structure.

thanks
 
mysql / phpmyadmin
 
Typically, you pass MySQL a SELECT query which fetches the greatest ID, increment it in code, then pass MySQL a program-generated INSERT query which creates the new record.

However, this can cause concurrency problems. In other RDBMSes you might use a stored procedure to fetch the old value and create the new one. But MySQL does not support stored procedures except in its alpha development version.


When doing this with MySQL, you typically use the auto_increment column for all table-relations and tracking of records. MySQL, when you add a row to a table that has an auto_increment column and MySQL generates a new auto_increment value, tracks on a per-connection basis the last auto_increment value generated.

Typically, you will pass MySQL the INSERT query and use mysql_insert_id() to fetch the auto_increment value generated.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
ok thanks. I'll try to put this in practice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top