我有一个MySQL数据库,在一列中,我有数字(价格),我需要用PHP计算此列中的所有数字...我该怎么做?
答案 0 :(得分:11)
如果要计算使用的列数:
Select count(column_name) FROM table name.
如果您想要列值的总和,请使用:
Select SUM(column_name) FROM table name.
答案 1 :(得分:1)
或许SUM是您真正想要的:
Select SUM(column_name) FROM tablename
答案 2 :(得分:0)
尝试使用mysqli_result(),
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($result = $mysqli->query("SELECT COUNT(price) as total FROM tablename")) {
/* Get field information for column 'SurfaceArea' */
$finfo = $result->fetch_field_direct(1);
printf("Total price: %s\n", $finfo->total);
$result->close();
}
/* close connection */
$mysqli->close();
?>
答案 3 :(得分:0)
您可以使用它来计算数据库中的1行:
<?php
$query = "SELECT COUNT(price) FROM yourtable ";
?>
然后,您可以在代码中使用$query
来显示结果。
在mysql(phpmyadmin)中你可以这样做:
SELECT COUNT(price) FROM yourtable
答案 4 :(得分:0)
使用PHP代码更新
如果你想算数,你可以这样做:
$query = "SELECT COUNT(*) FROM table_name"
// Print out result
while($row = mysql_fetch_array($result)){
echo "Total count". $row['COUNT(*)'];
}
如果你想得到一列的总和,你可以这样做:
$query = "SELECT SUM(column_name) FROM table_name"
// Print out result
while($row = mysql_fetch_array($result)){
echo "Total sum". $row['SUM(column_name)'];
}