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 9192615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:58:05+00:00 2026-06-17T20:58:05+00:00

I’ve been coding a new dashboard, where I want to show various DB stats,

  • 0

I’ve been coding a new dashboard, where I want to show various DB stats, mainly counting rows and setting the results into variables, its all working correctly, but I am a bit concerned that so many SELECT queries could become too heavy if a lot of users are entering or refreshing the page.

Appreciate your input 🙂

$tbl_players = players;

$xbox = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'XBOX360'");
$ps3 = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'PS3'");
$pc = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'PC'");
$xbfa = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'XBOX360' && fathread LIKE 'http%'");
$psfa = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'PS3' && fathread LIKE 'http%'");
$pfa = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'PC' && fathread LIKE 'http%'");
$xbcr = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'XBOX360' && crthread LIKE 'http%'");
$pscr = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'PS3' && crthread LIKE 'http%'");
$pcr = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'PC' && crthread LIKE 'http%'");
while($row = mysql_fetch_array($xbox))
    {
$cxbox = mysql_num_rows($xbox);
}
while($row = mysql_fetch_array($ps3))
    {
$cps3 = mysql_num_rows($ps3);
}
while($row = mysql_fetch_array($pc))
    {
$cpc = mysql_num_rows($pc);
}
while($row = mysql_fetch_array($xbfa))
    {
$xboxfa = mysql_num_rows($xbfa);
}
while($row = mysql_fetch_array($psfa))
    {
$ps3fa = mysql_num_rows($psfa);
}
while($row = mysql_fetch_array($pfa))
    {
$pcfa = mysql_num_rows($pfa);
}
while($row = mysql_fetch_array($xbcr))
    {
$xboxcr = mysql_num_rows($xbcr);
}
while($row = mysql_fetch_array($pscr))
    {
$ps3cr = mysql_num_rows($pscr);
}
while($row = mysql_fetch_array($pcr))
    {
$pccr = mysql_num_rows($pcr);
}

$tbl_interactive = interactive;

$maximum = mysql_query("SELECT ID FROM $tbl_interactive ORDER BY ID DESC LIMIT 1");
while($max = mysql_fetch_array($maximum))
{
$cint = $max['ID'];
}
$xboxf = mysql_query("SELECT * FROM $tbl_interactive WHERE Console = 'XBOX360' && Type = 'Player' && $date < EndTime");
$xboxc = mysql_query("SELECT * FROM $tbl_interactive WHERE Console = 'XBOX360' && Type = 'Club' && $date < EndTime");
$ps3f = mysql_query("SELECT * FROM $tbl_interactive WHERE Console = 'PS3' && Type = 'Player' && $date < EndTime");
$ps3c = mysql_query("SELECT * FROM $tbl_interactive WHERE Console = 'PS3' && Type = 'Club' && $date < EndTime");
$pcf = mysql_query("SELECT * FROM $tbl_interactive WHERE Console = 'PC' && Type = 'Player' && $date < EndTime");
$pcc = mysql_query("SELECT * FROM $tbl_interactive WHERE Console = 'PC' && Type = 'Club' && $date < EndTime");
while($row = mysql_fetch_array($xboxf))
    {
$xboxfi = mysql_num_rows($xboxf);
}
while($row = mysql_fetch_array($xboxc))
    {
$xboxci = mysql_num_rows($xboxc);
}
while($row = mysql_fetch_array($ps3f))
    {
$ps3fi = mysql_num_rows($ps3f);
}
while($row = mysql_fetch_array($ps3c))
    {
$ps3ci = mysql_num_rows($ps3c);
}
while($row = mysql_fetch_array($pcf))
    {
$pcfi = mysql_num_rows($pcf);
}
while($row = mysql_fetch_array($pcc))
    {
$pcci = mysql_num_rows($pcc);
}

Many Thanks!

  • 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-17T20:58:07+00:00Added an answer on June 17, 2026 at 8:58 pm

    Yes, that is too many queries, way too much overhead, and too much heavy lifting to get the resultset you want.

    You could get all nine of those counts from the $tbl_players table with a single statement, and on a single row. Those nine separate statements you have are preparing nine separate resultsets, and you’re fetching all those rows in the resultset to the client just to get a count. (Maybe there’s some optimization in there somewhere, but MySQL is preparing all those rows to return to the client.) It would be much more efficient to have MySQL just return the counts you want, using a statement something like this:

    SELECT SUM(IF(p.Console = 'XBOX360' ,1,0))                             AS xbox
         , SUM(IF(p.Console = 'PS3'     ,1,0))                             AS ps3
         , SUM(IF(p.Console = 'PC'      ,1,0))                             AS pc
         , SUM(IF(p.Console = 'XBOX360' AND p.fathread LIKE 'http%' ,1,0)) AS xbfa
         , SUM(IF(p.Console = 'PS3'     AND p.fathread LIKE 'http%' ,1,0)) AS psfa
         , SUM(IF(p.Console = 'PC'      AND p.fathread LIKE 'http%' ,1,0)) AS pfa
         , SUM(IF(p.Console = 'XBOX360' AND p.crthread LIKE 'http%' ,1,0)) AS xbfr
         , SUM(IF(p.Console = 'PS3'     AND p.crthread LIKE 'http%' ,1,0)) AS pscr
         , SUM(IF(p.Console = 'PC'      AND p.crthread LIKE 'http%' ,1,0)) AS pcr
      FROM $tbl_players p
    

    That gets you back a single row, with nine values. That would cut down on the amount of code you’ve got considerably, and significantly improve performance.

    Similarly, you can get all those counts from the $tbl_interactive table with a single statement as well:

    SELECT SUM(IF(t.Console = 'XBOX360' AND t.Type = 'Player' ,1,0)) AS xbfi
         , SUM(IF(t.Console = 'XBOX360' AND t.Type = 'Club'   ,1,0)) AS xbci
         , SUM(IF(t.Console = 'PS3'     AND t.Type = 'Player' ,1,0)) AS psfi
         , SUM(IF(t.Console = 'PS3'     AND t.Type = 'Club'   ,1,0)) AS psci
         , SUM(IF(t.Console = 'PC'      AND t.Type = 'Player' ,1,0)) AS pcfi
         , SUM(IF(t.Console = 'PC'      AND t.Type = 'Club'   ,1,0)) AS pcci
      FROM $tbl_interactive t
     WHERE t.Console IN ('XBOX360','PS3','PC')
       AND t.Type IN ('Player','Club')
       AND $date < t.EndTime
    

    while($row = mysql_fetch_array($tbl_interactive_counts))
    {
        $xboxfi = $row["xbfi"];
        $xboxci = $row["xbci"];
        $ps3fi  = $row["psfi"];
        $ps3ci  = $row["psci"];
        $pcfi   = $row["pcfi"];
        $pcci   = $row["pcci"];
    }
    

    FOLLOWUP:

    It’s possible that the SQL statements above to return a NULL rather than a zero, when there are no rows that satisfy the predicates (that is, match the WHERE clause). To have the statement return a count of zero in place of the NULL, we can use the MySQL IFNULL() function as convenient shorthand:

    SELECT IFNULL(SUM(IF(t.Console = 'XBOX360' AND t.Type = 'Player' ,1,0)),0) AS xbfi
         , IFNULL(SUM(IF(t.Console = 'XBOX360' AND t.Type = 'Club'   ,1,0)),0) AS xbci
         , IFNULL(SUM(IF(t.Console = 'PS3'     AND t.Type = 'Player' ,1,0)),0) AS psfi
         , IFNULL(SUM(IF(t.Console = 'PS3'     AND t.Type = 'Club'   ,1,0)),0) AS psci
         , IFNULL(SUM(IF(t.Console = 'PC'      AND t.Type = 'Player' ,1,0)),0) AS pcfi
         , IFNULL(SUM(IF(t.Console = 'PC'      AND t.Type = 'Club'   ,1,0)),0) AS pcci
      FROM $tbl_interactive t
     WHERE t.Console IN ('XBOX360','PS3','PC')
       AND t.Type IN ('Player','Club')
       AND $date < t.EndTime
    

    Note that we’ve just wrapped an expression: IFNULL(expr,0)

    That’s just a shorthand equivalent to: IF(expr IS NULL, 0, expr)

    also equivalent to the ANSI standard: CASE WHEN expr IS NULL THEN 0 ELSE expr END

    (Using IFNULL, we only have to specify expr one time.)


    Or, you could let MySQL return the NULL, and handle the replacment of the NULL with a zero in PHP. (Note that fetched value in the $row array will still contain the NULL, but a zero will be assigned to the scalar:

    $xboxfi = is_null($row["xbfi"]) ? 0 : $row["xbfi"] ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I want to show the soap response to UIWebview.. my soap response is, <p><img
I have a jquery bug and I've been looking for hours now, I can't
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I'm interested in microtypography issues on the web. I want a tool to fix:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have been unable to fix a problem with Java Unicode and encoding. The

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.