我有这段代码存储来自总列table_2013
的总和并将其存储到$total2013
。现在我想要回显这个总和,我想要回显total2013
加上total2014
之和的总和,而不是打印我得到的总和
“资源ID#4”
$t2013 ="SELECT SUM(total) FROM table_2013";
$total2013 = (mysql_fetch_assoc($t2013));
$t2014 ="SELECT SUM(total) FROM table_2014";
$total2014 = (mysql_fetch_assoc($t2014));
echo "$total2013";
echo "$total2014";
echo "$total2013 + $total2014";
答案 0 :(得分:0)
您没有执行查询。执行它们 -
$t2013 ="SELECT SUM(total) FROM table_2013";
$total2013 = (mysql_fetch_assoc(mysql_query($t2013)));
$t2014 ="SELECT SUM(total) FROM table_2014";
$total2014 = (mysql_fetch_assoc(mysql_query($t2014)));
您还需要提供列名称 -
echo $total2013['SUM(total)'] + $total2014['SUM(total)'];
答案 1 :(得分:0)
请试试这个:
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$t2013 ="SELECT SUM(total) as total2013 FROM table_2013";
$result2013=mysqli_query($con,$t2013);
// Associative array
$row2013=mysqli_fetch_assoc($result2013);
$total2013 = $row['total2013'];
$t2014 ="SELECT SUM(total) as total2014 FROM table_2014";
$result2014=mysqli_query($con,$t2014);
// Associative array
$row2014=mysqli_fetch_assoc($result2014);
$total2014 = $row['total2014'];
echo "total2013";
echo "total2014";
echo $toal = $total2013 + $total2014;
答案 2 :(得分:0)
您需要先运行查询:
$t2013 ="SELECT SUM(total) AS total FROM table_2013";
$q2013 = mysql_query($t2013);
$r2013 = mysql_fetch_assoc($q2013);
$total2013 = $r2013['total'];
$t2014 ="SELECT SUM(total) AS total FROM table_2014";
$q2014 = mysql_query($t2014);
$r2014 = mysql_fetch_assoc($q2014);
$total2014 = $r2014['total'];
echo "$total2013";
echo "$total2014";
echo "$total2013 + $total2014";
答案 3 :(得分:0)
您有两个错误。 1.您的查询未执行。 2.你正在回显一个字符串(echo“$ total2013 + $ total2014”;)。它应该回应你放在“......”中的同样的东西,它不会总结这两个变量。你应该回应如下:
echo $total2013 + $total2014;
由于