我有一個簡單的mysql表,其中'ratee'是用戶,評級是給予該用戶的評級。我想顯示該用戶被評分的次數以及這些評分的平均值。前者的工作方式如下面的代碼所示,但後者沒有。請問我哪裏錯了?我正在使用PHP。在PHP中返回MYSQL平均查詢的一個結果
//working
$sql = "SELECT * FROM ratings WHERE ratee='" . $user1 . "'";
$result = mysqli_query($conn, $sql);
$ratingsqty = mysqli_num_rows($result);
echo $ratingsqty;
//not working 1
$sql = "SELECT * FROM ratings WHERE ratee='" . $user1 . "'";
$result = mysqli_query($conn, $sql);
$rating = mysqli_avg($result);
echo $rating;
//not working 2
$sql = "SELECT avg(rating) FROM ratings WHERE ratee='" . $user1 . "'";
$rating = mysqli_query($conn, $sql);
echo $rating;
使用'mysqli_error'找出如果你是收到錯誤消息。我還建議跳過引用並利用API來防止SQL注入。由於您使用的是mysqli,請利用[prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和[bind_param](http://php.net/手動/ EN/mysqli的-stmt.bind-param.php)。 – aynber
嘗試傾銷'mysqli_error($ conn)' –
[** mysqli_avg **不存在。最近的匹配:](http://ca3.php.net/manual-lookup.php?pattern=mysqli_avg&scope=quickref) –