Jump to content


Photo

Assign query result to variable and add query results together


  • Please log in to reply
1 reply to this topic

#1 johnny

johnny
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 15 August 2006 - 07:00 PM

Ok I have the following two queries that return a numeric value:

QUERY 1:

$query="
SELECT CelebTotal.$week
FROM CelebTotal,schedule
WHERE CelebTotal.Celeb = schedule.f1
AND schedule.week = $week
AND schedule.team = '1'
";
$res = mysql_query($query);
while($row = mysql_fetch_assoc($res))
{
echo $row [$week].'<br>';
}

QUERY 2:

$query="
SELECT CelebTotal.$week
FROM CelebTotal,schedule
WHERE CelebTotal.Celeb = schedule.f2
AND schedule.week = $week
AND schedule.team = '1'
";
$res = mysql_query($query);
while($row = mysql_fetch_assoc($res))
{
echo $row [$week].'<br>';
}

I want to build a THIRD QUERY and I want that to return the SUM of the first two. I realize that I will likely have to build in the first two queries into this third query, so I've tried this:

$query="
SELECT CelebTotal.$week
FROM CelebTotal,schedule
WHERE CelebTotal.Celeb = schedule.f1
AND schedule.week = $week
AND schedule.team = '1'
";

$res=mysql_query($query);
while($rows = mysql_fetch_assoc($res));{
$f1 = $rows[$week];
};

$query2="
SELECT CelebTotal.$week
FROM CelebTotal,schedule
WHERE CelebTotal.Celeb = schedule.f3
AND schedule.week = $week
AND schedule.team = '1'
";

$res2=mysql_query($query2);
while($rows = mysql_fetch_assoc($res));{
$f2 = $rows[$week];
};

$total= $f1 + $f2;

print $total;

But that comes back Resource Id #3.

I have tried to do this:
$res = mysql_query($query);
$res2 = mysql_query($query2);
$total = $res + $res2;
print $total;

but that always returns a numeric value of 7. the answer is not 7. heck I can change the queries to go fetch out different cells in the table and it still comes back 7.

Help!

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 August 2006 - 08:26 PM

Resource? That means you're dealing with the PHP object itself, not the value... BTW, there's probably a way to do this in a single query, but I'm a little busy now, and it's not popping into my head yet.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users