也許這個問題是重複的。但我找不到我的問題的答案。 我得到這個問題Fatal error: Call to undefined method mysqli_stmt::get_result() in demo.php on line 24
行24在代碼$tasks = $stmt->get_result();
致命錯誤:調用未定義的方法mysqli_stmt :: get_result()在demo.php上線24
這裏是我demo.php這部分代碼。
<?php
/**
* Class to handle all db operations
* This class will have CRUD methods for database tables
*
* @author Ravi Tamada
* @link URL Tutorial link
*/
class Demo {
private $conn;
function __construct() {
require_once dirname(__FILE__) . '/include/db_connect.php';
// opening db connection
$db = new DbConnect();
$this->conn = $db->connect();
}
public function getAllChatRooms() {
$stmt = $this->conn->prepare("SELECT * FROM chat_rooms");
$stmt->execute();
$tasks = $stmt->get_result();
$stmt->close();
return $tasks;
}
public function getAllUsers() {
$stmt = $this->conn->prepare("SELECT * FROM users");
$stmt->execute();
$tasks = $stmt->get_result();
$stmt->close();
return $tasks;
}
public function getDemoUser() {
$name = 'AndroidHive';
$email = '[email protected]';
$stmt = $this->conn->prepare("SELECT user_id from users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
$num_rows = $stmt->num_rows;
if ($num_rows > 0) {
$stmt->bind_result($user_id);
$stmt->fetch();
return $user_id;
} else {
$stmt = $this->conn->prepare("INSERT INTO users(name, email) values(?, ?)");
$stmt->bind_param("ss", $name, $email);
$result = $stmt->execute();
$user_id = $stmt->insert_id;
$stmt->close();
return $user_id;
}
}
}
?>
在我的index.php我有代碼來調用該函數。 這是示例代碼
<?php
$chatrooms = $demo->getAllChatRooms();
foreach ($chatrooms as $key => $chatroom) {
$cls = $key == 0 ? 'selected' : '';
?>
<li id="<?= $chatroom['chat_room_id'] ?>" class="<?= $cls ?>">
<label><?= $chatroom['name'] ?></label>
<span>topic_<?= $chatroom['chat_room_id'] ?></span>
</li>
<?php
}
?>
請幫我構造這段代碼。
謝謝。
你爲什麼不使用stmt- $>錯誤顯示數據庫錯誤。 – Deep