Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strings with Numbers, I want the string, not the number. 1

Status
Not open for further replies.

Twenty7

Programmer
May 13, 2002
9
0
0
US
Hello,
I have a user profile script that I just went live with, and there is a minor bug that I can't figure out a way around very easily, just calling on the help of others as I've searched the web and can't quickly find a solution.

Each account in the mysql database is permanently identified by a unique number that never changes. Each account also has aliases that the account can be identified by, but they can change.

When a user logs in, they can enter either their name or their ID. The first thing I want to do when they submit the form is to determine their unique account ID. I have a mysql select statement with a where claus like the following...
#$string being the input from the form

where ID='$string' A1='$string' A2='$string'

the column ID being type int, and A1 and A2 being char.
This works fine for validating that the account actually exisits and grabbing the ID, until someone has the alias '7hm', or '007_sniper'. Because the numbers are at the begining of the string, the string is converted to the numeric value of '7', so if the User '7hm' in actuality has the ID of '145', it will pull in the ID '7'

I don't have to include the ID in the where claus if I can determine that the user did NOT enter an ID. I'm not sure how to determine if the user entered an ID or an alias. Can someone help me through this, or does anyone see a better way around this?

I'd just like to say thanks for reading, and that I appreciate your help in this. If there is something I didn't explain clearly or if I left something out, just let me know.

-Twenty7
-Twenty7
 
Ahhhhhh whoops, the where claus should be...
where ID='$string' OR A1='$string' OR A2='$string'

....
-Twenty7
 
Not good at perl at all here but, borrowing from php, you test your user input to see if it's an integer/number (php's is_int() comes to mind)? If it is an integer, go with it being a user ID. Or, from a different angle, test each character of the array/string and see if it's an integer.

Sorry to be so vague, but I know exactly ZERO perl. Kind of going on theory, hoping someone will back me up ;-) --
JR
 
Thanks for your response. When I encountered the problem I immediately thought of the PHP is_int function, and I tried to search for one in perl, but to no avail.

I was introduced to PHP after I started this project, I wish I was lucky enough to stumble onto it earlier. I plan on converting my script to PHP in the future, as I've already fallen for PHP over perl, but that is a whole other disscussion. PHP is so much more powerful over perl. -Twenty7
 
You can check if [tt]$string[/tt] contains all numbers using a regular expression. Something like this would work:

[tt]if ($string =~ /^\d+$/) {
# do ID=$string SQL
} else {
# don't do ID=$string SQL
}
[/tt]
 
Ahhhh, this is exactly what I was looking for. I've been working with solely with PHP for the last couple weeks, I completely forgot about the =~ operator.
Thanks mate ;) -Twenty7
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top