Enrolment system is only one of many systems who need to have or generate unique ID number. Example of this is your student number, employee number which is automatically generated by the computer to give you a unique identification. And you as a programmer you will use this unique identification of the particular student to serve as your primary key.

Well, okay now this is my code on how to generate your own ID, or we can call it primary key.

Let’s say that CNH-0000 is your format ID.

First let me give you some definition of terms here.

Primary key is a field in your database where data must be unique.

Db is the name of your database.

Tb is your database table.

Client_num is the primary key.

Substr() is a PHP function to get a portion of a string.

Mysql_connect() isa  php function to connect in the mysql database.

Mysql_select_db() is a php function to select a particular database.

Mysql_query() is a php function  which let you query something in your database.

Mysql_num_rows() is a php function that counts the result of your query.

Mysql_fetch_array() is a php function use to get an array of data from your database.

Well, here are the codes:

Mysql_connect(“localhost”);

Mysql_select_db(“db”);

$query=mysql_query(“Select client_num from client_info order by client_num desc”);

$result=mysql_num_rows($query);

if($result>0)

{

$user_num=mysql_fetch_array($query);

$num=substr($user_num[‘client_num’],4,4);

$num2=$num+10001;

$client_id=”CNH-“.substr($num2,1,4);

}

 

As you noticed I used this query “Select client_num from tb order by client_num desc” with order by desc to get the last client_num in my table tb. Now all you have to do is to make a code to print out your generated ID.

Share on Google+Tweet about this on TwitterShare on FacebookShare on TumblrShare on StumbleUponPin on PinterestShare on LinkedInDigg thisShare on RedditEmail this to someone