-1

I made this code to sum three fields called (linkhit) from three tables called( sound_link / sound_linkK/ sound_linkD in same database, but the result echo the word total not the number result by sum equation. So if this code is correct or not, please correct it to me if this is not correct.

<?php include "config.php"; ?> 
<?php $results = mysql_query("select sum(linkhit),as total
from (
    select linkhit
    from Sound_link
    union
    select linkhit
    from Sound_linkK
    union
    select linkhit
    from Sound_linkD
)") ; 
echo 'total'; 
?>
benRollag
  • 1,219
  • 4
  • 16
  • 21
ameer
  • 1
  • 1

2 Answers2

2
  • You have an unnecessary , after the sum function, it should be sum(linkhit) as total, not sum(linkhit) as total
  • mysql_query doesn't return the actual results, just a resource that you can read rows from, using functions like mysql_fetch_row.
  • As Madison pointed out, you want to use UNION ALL and not UNION
  • As Dave pointed out, once you do use mysql_fetch_row properly to get an array with the row from the result set, you want to use $row['total'] - using 'total' will simply get you a 'total' string, nothing else

What I understand from this questions it that you seems to know nothing or very little about PHP. From the request at the end, "if so please correct it to me", it seems like you are not interested in learning it, too. Why are you trying to program in a language you don't know nor interest to learn?

I don't think stackoverflow is a place where people write code for other people, its a place where programmers come asking for help. It doesn't seem like the reason you came here. If you want someone to write this for you, pay him. -1.

shesek
  • 4,584
  • 1
  • 28
  • 27
1

I don't know PHP, but how about echo $results("total"), or something along those lines?

Dave
  • 4,546
  • 2
  • 38
  • 59