TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-25-2007, 09:42 PM   #1 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default MySQL: Selecting a username?

Hey,

I've been trying to figure out just what would be the quickest way to selecting a username from a database, there's no chance of selecting through fulltext as some usernames are less than 3/4 characters and to me the standard select could be slow with a lot of members. It has to be selected as a username to get the ID.

Anyone have any other suggestions?
Thanks!
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 11-25-2007, 09:50 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I don't understand fully, why wouldn't a standard SELECT be of use? If you index the column, it shouldn't be slow.

SQL Code:
SELECT id, username, blah
FROM users
WHERE username = 'salathe';
Salathe is offline  
Reply With Quote
Old 11-25-2007, 10:27 PM   #3 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

Ah, but, is there any better way of doing it or is that the best way possible?
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 11-26-2007, 12:11 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

How can you have a better way? Set the user name field as an index and specify that it's unique. Can't get much faster than that.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-26-2007, 12:15 AM   #5 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

Ah, okay, thanks :)
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 11-26-2007, 09:04 AM   #6 (permalink)
The Wanderer
PHP Guru Advanced Programmer Zend Certified 
 
DragonBe's Avatar
 
Join Date: Nov 2007
Location: according to my wife: on the Net
Posts: 19
Thanks: 0
DragonBe is on a distinguished road
Default

Hi there,

If you know your way around MySQL, you can create an index on a part of a column so you can actually index the first three or four characters (do mind yourself you cannot make this an unique index)

The statement shown here creates an index using the first 4 characters of the name column:

CREATE INDEX part_of_name ON customer (name(4));

Ref: http://dev.mysql.com/doc/refman/5.1/...ate-index.html

Cheers,

DragonBe
Send a message via ICQ to DragonBe Send a message via Skype™ to DragonBe
DragonBe is offline  
Reply With Quote
Old 11-26-2007, 12:20 PM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Additionally, you may want to read Wildhoney's article about Sprintf:
Securing your MySQL Queries with Sprintf

More secured that way
Tanax is offline  
Reply With Quote
Old 11-26-2007, 08:57 PM   #8 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

Firstly, the table was already indexed, just thought there might be a better way and sprintf doesn't instantly make it secure, my method is secure as it is and does not use sprintf.
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 11-27-2007, 01:06 AM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Use EXPLAIN before your code. Like so:

sql Code:
EXPLAIN SELECT id, username, blah
FROM users
WHERE username = 'salathe';

You want to avoid ALL queries. But on an index it will be super fast anyway.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-27-2007, 10:45 PM   #10 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

you could use a stored precedure as well. this away the query is pre-compiled saving you that time.
CoryMathews is offline  
Reply With Quote
Old 11-28-2007, 04:18 AM   #11 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I tried stored procedures a while ago. Funny thing is, I know how to code using stored procedures, I just can't seem to get them to behave well with PHP. When I last tried, roughly a year ago, there were so many bugs in PHP relating to stored procedures - connections kept getting dropped and other miscellaneous issues, that I just gave up in the end. And that was with mysqli, too!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-28-2007, 07:11 PM   #12 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

i haven't done it in php yet, its on my todo list but i have done them in coldfusion and they were pretty easy. but most things seem to be harder in php.
CoryMathews is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:11 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design