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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:15:59+00:00 2026-05-16T23:15:59+00:00

I have different queries fetching from different tables in one single page. i want

  • 0

I have different queries fetching from different tables in one single page. i want to cut down the database tripping.

here is my code..

$query = "SELECT COUNT(*) as cnt_news FROM news";
$result = mysql_query($query);
$row = mysql_fetch_array($result);


$query = "SELECT COUNT(*) as cnt_adv FROM advertisements";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

$query = "SELECT COUNT(*) as cnt_comm FROM comments";
$result = mysql_query($query);
$row = mysql_fetch_array($result);


$query = "SELECT SUM(CASE WHEN c.approve = '1' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_approved,
          SUM(CASE WHEN c.approve = '0' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_unapproved,
          SUM(CASE WHEN c.spam = '1' THEN 1 ELSE 0 END) AS cnt_spam
          FROM COMMENTS c";
$result = mysql_query($query);
$row = mysql_fetch_array($result);


$query = "SELECT SUM(a.amount) as t_amnt,
          SUM(a.cashpaid) as t_cpaid,
          SUM(a.balance) as t_bal
          FROM advertisements a";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

i am confused on how to make the query to select the values from three different tables, news, advertisement and comments. how do i cut my code and database trips?

thank you.

  • 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-05-16T23:15:59+00:00Added an answer on May 16, 2026 at 11:15 pm

    You don’t have to cut your database trips, but you can cut your code by using a function

    To make a general purpose function would be a way better.

    <?php
    $num_news = dbgetvar("SELECT COUNT(*) FROM news");
    $num_adv  = dbgetvar("SELECT COUNT(*) FROM advertisements");
    $num_comm = dbgetvar("SELECT COUNT(*) FROM comments");
    
    function dbgetvar($query){
      $res = mysql_query($query);
      if (!$res) {
        trigger_error("dbget: ".mysql_error()." in ".$query);
        return FALSE;
      }
      $row = mysql_fetch_row($res);
      if (!$row) return NULL;
      return $row[0];
    }
    

    always make a function from repetitive code.

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

Sidebar

Related Questions

I have 3 different SQL queries from 3 different unrelated tables (all using LIMIT
I have a LINQ to SQL DataContext that queries four different tables. But I
Sometimes I have to run multiple queries (one after another) with slightly different arguments
I have different php output in jQuery-based tabs. This output is formed from database
I have different select boxes. I want to select second one with related id.
I have two different queries in my php page for the same table, the
I have some different SELECT queries with same values. I want to use something
I have a page with 3 layers, one for navigation, one for database records
Aim to Achieve : I want to have 3 different dataTables from 3 different
I have two different queries: SELECT PB_BANK_CODE, PB_BANK_NAME FROM GLAS_PDC_BANKS WHERE PB_COMP_CODE='1' AND PB_BANK_CODE='025'

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.