Hi there,
i am using the following code to count, and to sum the values from the database.
$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 = '0' 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";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
the following code gives me an error, the error is
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) AS cnt_news, COUNT(a.*) AS cnt_adv, COUNT(c.*) AS cnt_c' at line 2
if i remove the first three lines of the select query, it does not show the error instead it prints the wrong values.
that is wrong with my code. ??
the following code works perfectly fine for me.
$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);
where am i going wrong?