Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25755

How do i make this huge splitted queries into one single query

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.

Upvotes: 0

Views: 127

Answers (3)

Your Common Sense
Your Common Sense

Reputation: 157872

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.

Upvotes: 1

shamittomar
shamittomar

Reputation: 46692

You can turn to using mysqli library instead of mysql library to connect to MySQL. The use mysqli::multi_query to run multiple queries at once and get results.

Almost all mysql_ functions are available as mysqli_ functions:

<?php
$link = mysqli_connect("localhost", "user", "password", "dbname");

/* check connection */
if (mysqli_connect_errno())
{
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//these are your queries. Put them all in a string separated by semicolon ;
$query  = "SELECT COUNT(*) as cnt_news FROM news;";
$query .= "SELECT COUNT(*) as cnt_adv FROM advertisements;";
$query .= "SELECT COUNT(*) as cnt_comm FROM comments;";

// Now execute multi query
if (mysqli_multi_query($link, $query))
{
    do {
        /* store first result set */
        if ($result = mysqli_store_result($link))
        {
            while ($row = mysqli_fetch_row($result))
            {
                printf("%s\n", $row[0]);
            }
            mysqli_free_result($result);
        }
        /* print divider */
        if (mysqli_more_results($link)) {
            printf("-----------------\n");
        }
    } while (mysqli_next_result($link));
}

/* close connection */
mysqli_close($link);
?>

I have shown example for your three queries only. You can put more queries in $query. This way there will be single request to MySQL.

Upvotes: 1

Gazler
Gazler

Reputation: 84180

$query = "SELECT
COUNT(n.*) as cnt_news,
COUNT(a.*) as cnt_adv,
COUNT(c.*) as cnt_comm,
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,
SUM(a.amount) as t_amnt,
    SUM(a.cashpaid) as t_cpaid,
    SUM(a.balance) as t_bal
FROM
news n, advertisements a, comments c";

That should do it in a single query.

Upvotes: 1

Related Questions