add prefix to user id using phpmyadmin

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

Moderators: macek, egami, gesf

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

add prefix to user id using phpmyadmin

Postby anu » 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 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:

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

Re: add prefix to user id using phpmyadmin

Postby jspmahavir » 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.

ex:

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

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

Re: add prefix to user id using phpmyadmin

Postby bachans » 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();
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


Return to “phpMyAdmin”

Who is online

Users browsing this forum: No registered users and 0 guests