0

Hi I'm new in PHP need some help, i want to generate auto registration code like this (e.g: 0001, 0002, ..) form data base if available in database auto plus one in last number if not start from 0000 it go at 9999 and stop

my sql table as

id | regisCode | title

query is

$query = mysql_query("Select * from accounts");
$result = mysql_num_rows($query);
if ($result >  0){

$row = mysql_fetch_array ($result);
} else{
$new = 0000;
}

$account = substr('0000', 1);

$faccount = 1+$account;


echo "<h1>". $faccount ."</h1>";

sorry for poor english

4

1 に答える 1

4
$query = "SELECT MAX(cast(registration_code as decimal)) id FROM accounts ";  
    if($result = mysql_query($query))
    {
        $row = mysql_fetch_assoc($result);

        $count = $row['id'];
        $count = $count+1;

        $code_no = str_pad($count, 4, "0", STR_PAD_LEFT);
    }

これにより、0000,0001,0002 が生成されます...0099 に達すると、0100 が生成されます..

于 2013-09-13T10:29:50.463 に答える