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

  • Home
  • SEARCH
  • 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 8963843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:26:55+00:00 2026-06-15T16:26:55+00:00

I have two tables in one database. I am querying the first table limit

  • 0

I have two tables in one database. I am querying the first table limit by 10 then loop the results. And inside the while loop, I am doing again another query using a data from the first query as a parameter. Here is an example of the script:

<?php

    $con = mysql_connect(host,username,password);
    mysql_select_db(game_server);

    //My first query
    $q1 = mysql_query('SELECT * FROM game_characters ORDER BY score DESC LIMIT 10');

    while($character = mysql_fetch_object($q1)){
    //My second query
        $q2 = mysql_query('SELECT * FROM game_board WHERE id="'.$character->id.'"');
        $player = mysql_fetch_object($q2);
    }
?>

So if I have a result of 100 rows, then the second query will execute 100 times. And I know it is not good. How can I make it better. Is there a way to do everything in one query? What if there is another query inside the while loop where a data from the second query as a parameter is used?

P.S.: I am doing a rankings system for an online game.

  • 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-15T16:26:56+00:00Added an answer on June 15, 2026 at 4:26 pm

    You can do it in one query if you use JOINs.

    SELECT * FROM game_board AS b 
        LEFT JOIN game_characters AS c ON b.id = c.id 
    ORDER BY c.score DESC 
    LIMIT 10
    

    You can also use nested query

    SELECT * FROM game_board AS b WHERE 
      id IN (SELECT id FROM game_characters AS c ORDER BY score DESC LIMIT 10)
    

    You can also put all game_character.id into an array, and use

    $sql = "SELECT * FROM game_board AS b WHERE b.id IN (" . implode(', ', $game_character_ids) . ")";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I have is two tables inside of a mysql database. One table contains
In a MySQL database I have two tables linked in a join. One table
I have two tables in one database. id | title --------- // table name
I have two tables that I'm querying from. In one table, there are fields
I have two tables in my MySQL database, one is a library of all
I have two related tables in my database: Page and Tag. One Page can
I have one database table TABLE_TIPS with two value 1. tips_id (auto increment) 2.
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables : one table it matchs and the other is teams.
I have two MySQL database tables: one containing a list of championships and another

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.