How to increment username in php/mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
waqas87
Forum Newbie
Posts: 3
Joined: Sat Aug 21, 2010 5:16 pm

How to increment username in php/mysql

Post by waqas87 »

hi ..
i am working on chat system ..there are two types of user one are register and other are guest and i have to add guest user..in to database as temporary so they can chat with each other the problem i am facing is with there names i want name like this (Guest1,Guest2,Guest3,Guest4,...so on..)..i am trying but it is not working on live server .. i have tested it on my local server using xampp server.. it worked but not on live server...

here is the code which i am using

Code: Select all

      if(isset($_SESSION['Guest']))

      {
   
      $_SESSION['Guest']=$_SESSION['Guest']+1;

      }

      else

      {
 
      $_SESSION['Guest']=1;

if(isset($_SESSION['Guest'])) { $_SESSION['Guest']=$_SESSION['Guest']+1; } else { $_SESSION['Guest']=1; }
and one more thing how i can delete Guest users who are not online..
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: How to increment username in php/mysql

Post by MindOverBody »

Advice is to use auto_increment ID on database table, and upon inserting new guest into db, check for biggest ID in table (do some google magic with MySQL 'MAX' keyword), bind 'Guest' word with MAX_ID+1 value, and that's it.
waqas87 wrote:and one more thing how i can delete Guest users who are not online..
Put 'last_activity' column in regarding database table which will be updated on every user/guest action, and on users 'login' check if there are users/guests with last activity that is by your opinion enoguh to declare someone 'offline'. Do time_now - last_activity difference to check it.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to increment username in php/mysql

Post by requinix »

Don't bother with making sure the first guest is "Guest1" and the second is "Guest2". Just make them unique.

Insert the new row into the table and call them "Guest" + whatever the auto-incremented ID is.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: How to increment username in php/mysql

Post by shawngoldw »

The problem is that $_SESSION variables are different for each user. You can not just check if a session variable is set and increment it because each user has their own session variables. You need to use a database.

Shawn
waqas87
Forum Newbie
Posts: 3
Joined: Sat Aug 21, 2010 5:16 pm

Re: How to increment username in php/mysql

Post by waqas87 »

thx for reply guys ...guest problem solved ...how i can check guest is online or not i am using this

Code: Select all

					$delete = "DELETE FROM tbl_GuestUser WHERE GuestLogtime < NOW() - INTERVAL 300 SECOND";
					$record = mysql_query($delete) or die("DELETE SQL query failed");
to delete guest from database but this delete every user..i have one field as GuestLogtime which store Guest login time ....and the sub from current time ..
Post Reply