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 06-27-2008, 10:31 PM   #1 (permalink)
The Visitor
 
Join Date: Jun 2008
Location: Zagreb, Croatia
Posts: 4
Thanks: 3
mfolnovich is on a distinguished road
Default getting position

So, I have mysql table users:
ID name score

and when getting position of user 'abc', I use:
Code:
$q = select * from users order by score; 
$o = mysql_query($q);
for($i=0;$r=@mysql_fetch_assoc($o);++$i)
    if ($r["name"] == $username) return true;
my question is how can I optimize it, I might have thousands of users ...
Send a message via ICQ to mfolnovich Send a message via AIM to mfolnovich Send a message via MSN to mfolnovich Send a message via Yahoo to mfolnovich
mfolnovich is offline  
Reply With Quote
Old 06-28-2008, 12:47 AM   #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

You might go for something within MySQL rather than looping through every result until you find the right one in PHP.

SQL Code:
SELECT
    ID, name, score,
    (
        SELECT COUNT(ID) + 1
        FROM users AS i
        WHERE i.score > o.score
    ) AS rank
FROM users AS o
WHERE name = 'abc'
ORDER BY score DESC

I did a quick test with 100,000 users and it seemed to work ok but would advise really examining the whole approach and see what works best for large data sets (PHP vs MySQL, different queries, etc.).

Apologies for any mistakes (or really bad SQL form!), it's 2am and I've just finished packing for my trip this evening... my mind's on other things, time to snooze for a while.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
mfolnovich (06-28-2008)
Old 06-28-2008, 09:45 AM   #3 (permalink)
The Visitor
 
Join Date: Jun 2008
Location: Zagreb, Croatia
Posts: 4
Thanks: 3
mfolnovich is on a distinguished road
Default

Ok, tnx, I'm going to wedding soon, so I'll test it when I get back, but when I just look at it, seems fine ...

EDIT: another question, if I have table messages:
ID name content read datetime
1 foo fooo ,1,2 12
2 foo2 fooo2 ,2 19
3 foo3 fooo3 ,3,1 16
4 foo4 fooo4 , 21

current userID is 1, when I sort it, I want to have:
ID name content read datetime
2 foo2 fooo2 ,2 19
4 foo4 fooo4 , 21
1 foo fooo ,1,2 12
3 foo3 fooo3 ,3,1 16

so first goes all messages that are not read (all which doesn't have current userID in `read`), and those are sorted with datetime ... how would query be ?

btw. sorry for my english ...

Last edited by mfolnovich : 06-28-2008 at 10:13 AM.
Send a message via ICQ to mfolnovich Send a message via AIM to mfolnovich Send a message via MSN to mfolnovich Send a message via Yahoo to mfolnovich
mfolnovich 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 02:42 AM.

 
     

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