以下代码给出了以下内容:
$getexpenses = "SELECT sum(expense_amount) FROM projects_expense WHERE project_id=$project_id";
$showexpenses = @mysqli_query ($dbc, $getexpenses); // Run the query.
echo ' . $showexpenses . ';
给我一个结果
' . $showexpenses . '
而不是实际的总和...
我确信这很简单,我很想念......谢谢!
答案 0 :(得分:2)
echo " . $showexpenses . "
将为您效劳。你需要双引号,而不是单引号。
答案 1 :(得分:1)
使用
echo $showexpenses;
或
echo "My query is: {$showexpenses}";
答案 2 :(得分:1)
我添加了对mysqli_fetch_assoc($showexpenses)
的调用以使其完全正常运行。
我还清理了$project_id
以避免注入并使用别名来通过关联数组访问总和。
$getexpenses = "SELECT SUM(`expense_amount`) as `sum_amount` FROM `projects_expense` WHERE `project_id`= ".intval($project_id);
$showexpenses = mysqli_query ($dbc, $getexpenses);
$sums = mysqli_fetch_assoc($showexpenses);
$sum = $sums['sum_amount'];
echo $sum;
我们也可以像这样使用mysqli_fetch_row
:
$getexpenses = "SELECT SUM(`expense_amount`) FROM `projects_expense` WHERE `project_id`= ".intval($project_id);
$showexpenses = mysqli_query ($dbc, $getexpenses);
$sums = mysqli_fetch_row($showexpenses);
$sum = $sums[0];
echo $sum;
因为我们知道第一列(0)是匹配的,所以不再需要别名。
注意:如果您想使用GROUP BY project_id
SUM()
答案 3 :(得分:0)
根据Nik的回应,PHP根据您是否使用单引号或双引号来区别对待字符串。
单引号将文本解释为文字文本,除了输出单引号的'\''和输出斜杠标记的'\'。因此,使用单引号将输出变量名而不是值。
示例:
CODE | OUTPUT
-----------------+------------------
echo '\t'; | \t
echo '\r'; | \r
echo '$foo'; | $foo
双引号解释特殊字符的某些转义序列,并解释变量。例如,“\ n”输出换行符,“\ r”输出回车符。因此,回显“$ myvariable”会输出值而不是变量名。
示例:
CODE | OUTPUT
-----------------+------------------
echo "\t"; | {tab space}
echo "\r"; | {carriage return}
echo "$foo"; | bar