0
當我運行此:PHP函數中的if語句(面向對象)的說法
if ($result->num_rows() > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
我收到以下錯誤:
Call to undefined method mysqli_result::num_rows()
我相信錯誤是從num_rows()
方法,但可以不太明白什麼是錯的。據我所知,在使用OOP對象$obj->foo()
調用方法,但是當我刪除的num_row
括號:
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
的代碼塊運行正常。
好吧。所以'num_rows'是由php創建的屬性,而不是一種方法? – Simon
是的。 http://php.net/manual/en/mysqli-result.num-rows.php和整個班級http://php.net/manual/en/class.mysqli-result.php –
你可以看到'$ num_rows'被定義爲一個屬性(aka屬性,成員變量等)。 –