Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8324211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:58:47+00:00 2026-06-08T23:58:47+00:00

I am pulling data from a mysql DB. I would like to add the

  • 0

I am pulling data from a mysql DB. I would like to add the length of multiple runs and sort them by the rank of the person that has run the furthest.

    function determineLength($db, $name){
      $getLength = "select length from $db where name = $name and sex = 'male'";
      $gettingLength = mysql_query($getLength);
      while($gotLenth = mysql_fetch_array($gettingLength)){

      more code that calculates total length and returns it as $lengthAccumulator 
      }
      return array('totalLength'=>$lengthAccumulator);
    } 

Now, I have 30 different males whose names will never change, that I need to pull and sort. How should I go about running the mysql code for each person without redundancy? I can only figure out this way –

    $name = john;
    $a = determineLength($db, $name);
    $aLength = $a['totalLength'];

    $name = sam;
    $b = determineLength($db, $name);
    $bLength = $b['totalLength'];

    $name = henry;
    $c = determineLength($db, $name);
    $cLength = $c['totalLength'];

    $name = ted;
    $d = determineLength($db, $name);
    $dLength = $d['totalLength'];

then store the $aLength, $bLength, $cLength…into an array and sort them out that way.
it just seems like the WRONG way to do it, not to mention redundant and slow. there are over 40k rows of data in the DB, so attempting to do it this way would run through the DB over 1.2 Mil times?! Can I pass an array of the names into the function, then utilize the ORDER BY length DESC in the mysql code?

I’m in need of help. Thank You!

    ****the answer below by zane seemed to work to order the runners, but   

how would I echo the actual rank using this method? I have substituted his select statement into the one I had above, but how would I go about echoing a persons rank? Could I save the results into an array then echo the key?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:58:50+00:00Added an answer on June 8, 2026 at 11:58 pm

    If I understand your situation correctly, you can simply do this in one single SQL statement:

    SELECT   name
    FROM     $db
    GROUP BY name
    ORDER BY SUM(length) DESC
    

    And everything is already sorted straight from the result-set. No procedural code.

    The above gets all runners, but if you wanted to get a sorted set of specific male runners, you can add a WHERE clause like so:

    SELECT   name
    FROM     $db
    WHERE    name IN ('name1', 'name2', 'name3', 'etc...') AND 
             sex = 'male'
    GROUP BY name
    ORDER BY SUM(length) DESC
    

    To rank in the actual SQL, you can do:

    SELECT     name, @rank:=@rank+1 AS rank
    FROM       $db
    CROSS JOIN (SELECT @rank:=0) val_init
    WHERE      name IN ('name1', 'name2', 'name3', 'etc...') AND 
               sex = 'male'
    GROUP BY   name
    ORDER BY   SUM(length) DESC
    

    Then the reference the rank column in php for the person’s rank.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am pulling some data from mysql database using PHP, now I would like
It seems like I would be jumping through hoops to move data from MySQL
I have some code that pulls data from a mysql db everything is pulling
I have an INSERT query that is pulling data from two tables and inserting
Background: I'm building a small application that will be run daily, pulling data from
I am pulling information that has cities attached to it and i would like
I am pulling data down from a MySQL table and loading it into a
I am pulling some data from a mysql table via the following: $result =
Im pulling data from a MySql data table. I'm pulling from a row called
I have my jquery mobile app pulling data from our mysql db using JSONP.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.