无法添加两个mysql列并返回总数

时间:2015-01-28 01:05:16

标签: php mysql

我正在尝试添加mysql列,然后将总数列返回到php页面。我得到了代码,但它没有返回值:

<?
$pointresults2 = $dbh->prepare("SELECT sum(features + contributions + likes 
)   
AS total FROM points WHERE (ID = :user_ID)");
$pointresults->bindParam(':user_ID', $user_ID, PDO::PARAM_INT); 
$pointresults2->execute();
$pointrow2 = $pointresults2->fetchAll(PDO::FETCH_ASSOC);
?>

<h3><b>Points TEST: <?php echo $pointrow2[0]['total'] ?></b></h3>

1 个答案:

答案 0 :(得分:1)

您的情况中没有featurescontributionslikes。您也将错误的名称作为参数user_ID

<?php
$stmt = $dbh->prepare("SELECT sum(features + contributions + likes) AS total FROM points WHERE (ID = :user_ID)");
$stmt->bindParam(':user_ID', $user_ID, PDO::PARAM_INT); 
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);

echo '<h3>'. $user_ID .' have '. $row['total'] .' Points.</h3>';
?>