Trouble getting SQL SUM to work with $variable table name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Andyish56
    New Member
    • Oct 2014
    • 2

    Trouble getting SQL SUM to work with $variable table name

    Hi, I'm trying to return the sum of balances from a table and can get the following code to work when using a specific table name

    $qry = mysql_query("SE LECT SUM(Balance) AS total FROM table1 ");
    $row = mysql_fetch_ass oc($qry);
    echo $row['total'];

    The problem im having is that my table name changes and this needs to be a variable but when i use the following code I get no result

    $table="table1" ;
    $qry = mysql_query(" SELECT SUM(Balance) AS total FROM $table ");
    $row = mysql_fetch_ass oc($qry);
    echo $row['total'];

    Can anyone offer some help please?
  • Andyish56
    New Member
    • Oct 2014
    • 2

    #2
    Sorry I've found the error which was not in the code posted

    Comment

    Working...