Hello,

I am using the code below to print the sum of all the ListPrice values returned from the query. When I SSH into my server and run the query in MySQL it works just fine (replacing $id with a real value). However, my code below will not print out anything.

I am using PHP4, MySQL4 and the PEAR DB module.

Any help would be greatly appreciated.

Thanks in advance!

$TotalSql = "SELECT sum(ListPrice) FROM residential WHERE ListAgentId='$id'";
					$TotalRow = $db->query($TotalSql);

				echo "<tr><td>Total:</td><td colspan=\"2\">".$TotalRow['sum(ListPrice)']."</td></tr>";

    You might set this SUM with an alias.

    $TotalSql = "SELECT sum(ListPrice) AS price FROM residential WHERE ListAgentId='$id'";
    

    Have you forget the mysql_fetch_assoc ?

    	$TotalRow = $db->query($TotalSql);

      As has been noted, you need to actually fetch the data once you've SELECT'ed it. Try something like:

      $result = $TotalRow->fetch_assoc();

      after you query the DB and change your echo statement to use the $result associative array.

      EDIT: Just re-read your post and noticed that you're using the PEAR DB class. Sorry about that - my mistake. Instead of fetch_assoc(), it would be getAssoc(). Then again, if you're just SELECT'ing one column, there's really no need for an array at all; instead just use getOne().

        Write a Reply...