Jump to content


Photo

How can I add these query results?


  • Please log in to reply
4 replies to this topic

#1 johnny

johnny
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 14 August 2006 - 04:51 AM

Ok I have the following queries that display numeric values on my page:

      <?php

 $link = mysql_connect("xxx", "xxx", "xxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("xxx") or die("Could not select database");
$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>';
}



?>

      <?php

 $link = mysql_connect("xxx", "xxx", "xxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("xxx") or die("Could not select database");
$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 ADD these two values and display that number on the same page.  I have tried something like this, but it's not working:

<?php

 $link = mysql_connect("xxx", "xxx", "xxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("xxx") or die("Could not select database");
$query="
SELECT CelebTotal.$week
FROM CelebTotal,schedule
WHERE CelebTotal.Celeb = schedule.f1
AND schedule.week = $week
AND schedule.team = '1'
";

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

$res=mysql_query($query);
$res2=mysql_query($query2);

$total = $res+$res2;

echo $total;

?>

It returns a number, but that number is always 7 and that's not correct. 

#2 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 14 August 2006 - 05:55 AM

Try:

<?php

 $link = mysql_connect("xxx", "xxx", "xxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("xxx") or die("Could not select database");
$query="
SELECT CelebTotal.$week
FROM CelebTotal,schedule
WHERE CelebTotal.Celeb = schedule.f1
AND schedule.week = $week
AND schedule.team = '1'
";


$res=mysql_query($query);

$total = $res++;

echo $total;

?>
[/code[


#3 johnny

johnny
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 14 August 2006 - 08:43 PM

What does the "++" mean/do?  That code only includes one of the two queries I want to add together. 

#4 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 14 August 2006 - 09:43 PM

++ means post-increment. The line "$total = $res++" means "assign the value of $res to $total, and then increase $res by 1".
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#5 johnny

johnny
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 14 August 2006 - 09:48 PM

That is displaying "Resource id #3" - WTF is that?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users