add prefix to user id using phpmyadmin

How to use phpmyadmin.... phpmyadmin related arguments. Even phpMyAdmin issues

Moderators: egami, macek, gesf

Post Reply
User avatar
New php-forum User
New php-forum User
Posts: 66
Joined: Mon Mar 21, 2011 10:49 pm

Mon May 16, 2011 10:41 pm

hello all,

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

in this CL Is the 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:

User avatar
New php-forum User
New php-forum User
Posts: 14
Joined: Wed Apr 20, 2011 4:53 am
Location: Ahmedabad, Gujarat, India.

Thu May 19, 2011 11:44 pm

Hi Anu

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


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

New php-forum User
New php-forum User
Posts: 67
Joined: Thu May 19, 2011 6:01 am
Location: India

Fri May 20, 2011 5:43 am

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: Select all

$sql ="SELECT user_id FROM `users` ORDER BY timestamp DESC";
$result = mysql_query($sql);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
$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.
Bachan Smruty
Mindfire Solutions

Post Reply