我试图在mysql表中找到一列的总和,但我收到如下错误。
<?php require_once("../../../../512/1.001/data/class.php");
$sql=mysqli_query($db,"select SUM(tbl_ccp_loans.payableamount) AS total"); $row = mysqli_fetch_assoc($sql);
$sum = $row['total'];
$sum;?>
我希望总和是'数字',但输出是
警告:mysqli_query()要求参数1为mysqli,第223行/home/testing/public_html/orrf/bej/ccp-main2/_dashboard/data.php中给出的对象
警告:mysqli_fetch_assoc()期望参数1为mysqli_result,在第224行/home/testing/public_html/orrf/bej/ccp-main2/modules/_dashboard/data.php中给出null
函数mysqli_query()期望作为mysqli类的第一个参数对象(由mysqli_connect()返回),但是根据您的注释中的代码,您的$db
变量是不同类的对象。如果这是一个自定义类,请尝试使用此类中的适当方法来执行查询并检索结果。
接下来是使用mysqli_connect()
的基本示例。
<?php
require_once("../../../../512/1.001/data/class.php");
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$db) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
$sql = mysqli_query($db, "select SUM(tbl_ccp_loans.payableamount) AS total");
if ($sql === false) {
echo 'Error. ' . mysqli_connect_errno() . PHP_EOL;
exit;
}
$row = mysqli_fetch_assoc($sql);
$sum = $row['total'];
echo $sum;
?>