mysql sum函数没有给出结果

I am a bit confused in calculating sum of particular column using mysql.

Database Structure table name: personal there is 25 rows in total, and columns id, userid, name and amount.

I want to calculate amount of particular userid through running session.

Code:

<?php
$userid = $_SESSION['userid'];
$sql = "SELECT userid, SUM(amount) FROM personal";
echo $sql."<br/>";
echo $userid;
?>

Output: SELECT userid, SUM(amount) FROM personal gold99

Please help..guys..

As you have written it you will get one arbitrary user name, followed by the sum of all amounts. If you want the sum for each user name you need to use SUM as an aggregate function with a GROUP BY:

SELECT userid, sum(amount) FROM personal GROUP BY userid

and as mentioned if you want the sum for just one user, use a WHERE clause.
But getting the query right is only part of the problem. You also need to use php functions to execute that query against some database. This is normally done either with mysqli_ APIs or PDO (more object-y and works against various database providers). You'll need to access a database, create a statement, execute it, and fetch the results. See http://php.net/manual/en/book.mysqli.php and http://php.net/manual/en/book.pdo.php for more, and give it a try on your own.

try

<?php
    $userid = $_SESSION['userid'];
    $sql = "SELECT userid, SUM(amount) as amount FROM personal where userid=$userid";
    //echo $sql."<br/>";

    //you must execute query
    $query=mysql_query($sql);
    $result=mysql_fetch_array($query);

    echo $result['amount'];
    ?>

Use this Query it will work :

SELECT SUM(amount) as amount FROM personal WHERE userid='xyz'
<?php
$query = "SELECT userid,amount, SUM(amount) FROM personal WHERE sponserid='$_SESSION[userid]'"; 

$result = mysqli_query($conn,$query);
$count = mysqli_num_rows($result);
$i=1;
while($row = mysqli_fetch_array($result) or die(mysqli_error($conn)))
{
    echo "UserId:". "=". $row['userid'];
    echo "<br />";
    echo "Amount of Id". "=". $row['amount'];
    echo "<br />";
    echo "Sum of total Income"."=".$row['SUM(amount)'];


$i++;   } ?>