PDA

View Full Version : what this function do?


o0O0o.o0O0o
06-21-2008, 08:26 AM
hi ,


Previous programmer has programmed this function to get the next valid username(number) using this function i don't know what this function is doing . Can anyone help me on this


if (!isset($errmsg)) {
/* Lock table so other users can not be created */
$result = mysql_query ("lock table tblsequence write
") or die ("<CENTER><B>2. Fatal Error " . mysql_errno(). ":" . mysql_error() . "</B></CENTER<P>;");

/* Grab new customer reference and create check digit */
$result = mysql_query ("select username from tblsequence
") or die ("<CENTER><B>2. Fatal Error " . mysql_errno(). ":" . mysql_error() . "</B></CENTER<P>;");
$row=mysql_fetch_array($result);
if ($row) {
$num = $row["username"]+1;
$username = checkDigit($num);
} else {
die ("<CENTER><B>3. Fatal Error : Could not generate customer reference number.</B></CENTER<P>;");
}

/* Update username sequence */
$result = mysql_query ("update tblsequence set username = ".$num."
") or die ("<CENTER><B>2. Fatal Error " . mysql_errno(). ":" . mysql_error() . "</B></CENTER<P>;");

/* Unlock table so other users can be created */
$result = mysql_query ("unlock tables
") or die ("<CENTER><B>2. Fatal Error " . mysql_errno(). ":" . mysql_error() . "</B></CENTER<P>;");

/* Insert data into database */

Function chk digit is


function checkDigit ($Number) {
$sum = 0;
$numLength = strlen($Number);
ereg("([0-9]{0,1})([0-9]{0,1})([0-9]{0,1})([0-9]{0,1})([0-9]{0,1})([0-9]{0,1})([0-9]{0,1})([0-9]{0,1})([0-9]{0,1})([0-9]{0,1})", $Number, $ref);

for ( $i=$numLength ; $i>=1; $i-- ) {
if (isset($set)) {
$value = $ref["$i"];
$sum += $value;
unset($set);
} else {
$value = $ref["$i"] * 2;
if (ereg("^([0-9]{2})$", $value, $tmp)) {
ereg ("([0-9]{0,1})([0-9]{0,1})", $value, $array);
$value = $array[1] + $array[2];
}
$sum += $value;
$set = 1;
}
}
$checkDigit = ($sum%10);
if ($checkDigit != 0) {
$checkDigit = 10 - $checkDigit;
}
return $Number . $checkDigit;
}


The problem is my database was updated by mistake.

My table sequence was updated with database 1 day back
and tbluser stays the same

so now i have few users with same username

So my database is having userid field and username field both numbers.
I don't know why it was programmed like that.


Now the field userid is auto increment , but sometimes some greater value appears before the small value like

22345

22346

22348

22347

22350

i don't know how this can happen

Inigoesdr
06-21-2008, 01:27 PM
If you have some rows that were deleted that can happen. auto_increment uses the next value after that last row created, not necessarily the first available row id.

o0O0o.o0O0o
06-21-2008, 07:42 PM
If you have some rows that were deleted that can happen. auto_increment uses the next value after that last row created, not necessarily the first available row id.

but then how can next row after that will be less than previous one

NancyJ
06-21-2008, 08:42 PM
because they're not ordered by id.

o0O0o.o0O0o
06-24-2008, 01:14 AM
because they're not ordered by id.

what does that mean

gnomeontherun
06-24-2008, 01:38 AM
That means they aren't selected from the database and sorted by the number, they are just pulled out in the native order, which is not usually sorted in any particular way.

o0O0o.o0O0o
06-25-2008, 03:06 AM
Any idea what that chkdigit function does and why is that used


I have little idea if i have username = 2245 in tblsequence then function checkdigit

return 2245(n) and one more digit after to assign it to next username . I have no idea what that means and next register user will have username = 2246(n) where again n comes from that function