0

I have a little situation here. If in my database in my "Session" Table if I have the data below:

SessionId

C

Then what happens with my code is that it would not display the string above when user generates a new string as that it is already in the database. This works fine for example above

The problem I have though is that if I have multiple sessions(exams), then in the database below it will display the strings as below in the database:

SessionId

C1
C2
C3

The above is for 3 exams. But the problem is that the string "ADERF" can still be generated in the code below. What my question is that if there is a number at the end of the string like the table above, then how can I not generate the string as it is still in the database? The strings will always be 5 characters long:

user1723760
  • 1,157
  • 1
  • 9
  • 18
  • Strip off any trailing digits and check whether the resulting string is in the database? – Wyzard Nov 01 '12 at 00:42
  • Is there any particular reason the session needs to be five characters? – SomeKittens Nov 01 '12 at 00:44
  • @Wyzard If there are no digits at end of string, then it won't generate the string as it's already in db, thats fine, its when the the digits are at the end in the db then it still generates the string. Do I need to change the query to include a LIKE? could that work? – user1723760 Nov 01 '12 at 00:44
  • @SomeKittens No there is no reason why it has to be 5 characters, it is just that it gives session(exam) its own ID, thats all – user1723760 Nov 01 '12 at 00:45
  • Ah, I had it backward. Digits are in the database already, and you want to do the query using a string that doesn't have the digits. In that case, you'll want to use a regex in the query (not LIKE, since I don't think that can specify that certain characters must be digits). – Wyzard Nov 01 '12 at 00:48
  • @Wyzard can you show a sample answer on how to use the regex in my code? It will be very helpful and probably will solve the problem as I tried the LIKE statement but it did not work – user1723760 Nov 01 '12 at 01:37

2 Answers2

1

If you're looking to give each session/exam their own id (and not worry about headaches when they collide) try using GUIDs. There's virtually no chance of collision.

Community
  • 1
  • 1
SomeKittens
  • 38,868
  • 19
  • 114
  • 143
  • Read the link you gave me, never used GUID and afraid to use it because woah that is confusing. How can this be manipulated in my code? – user1723760 Nov 01 '12 at 01:12
  • http://php.net/manual/en/function.com-create-guid.php. Like most things, PHP's got a function for it. – SomeKittens Nov 01 '12 at 01:14
0

Change your query to:

$result = "SELECT SessionId FROM Session WHERE SessionId LIKE CONCAT(?, '%')";
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hi, I tried this and tested it with a smaller string but it still generates a string which is already in db. If In database my SessionId's are `C`, `C1`, `C2` and `C3`, it should not generate string `C` but it is still generating the string. So Im guessing the like statement is not working – user1723760 Nov 01 '12 at 01:11