add prefix to user id using phpmyadmin

A

Anonymous

Guest
hello all,

in my project i need to add the user id with prefix like CL1,CL2,........

in this CL Is the prefix.my question is how can i do this?i mean how can i add the prefix to the id field using phpmyadmin?

or else i want to change the coding?

help me..... :help:
 
Hi Anu

For adding prefix from database then u want prepared query with concat function in sql query.

ex:

SELECT concat( 'CL', '#', user_id )
FROM `users`
LIMIT 0 , 30
 
Hi Anu,

If you are using int type for user_id, then change it to varchar type and add a new field timestamp(which will store the current time). Next when you are going to add a new record, prior to that just fetch the latest record that is in the database. Please check the following code.

Code:
$sql ="SELECT user_id FROM `users` ORDER BY timestamp DESC";
$result = mysql_query($sql);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);

if(empty($row)) {

// first record is going to insert. so give the user id

$user_id = 'CL1';
} else {
$pre_user_id = $row['user_id'];
$num_pre_user_id = substr($pre_user_id,2); // It will get the numeric part of the user_id
$user_id = (int)$num_pre_user_id + 1;
$user_id = 'CL'.$user_id;
}

// Now you will have your user_id. Just insert the new record which contains the new user_id.

Thanks
Bachan Smruty
Mindfire Solutions
http://www.mindfiresolutions.com
 
Back
Top