2016-03-03 86 views
0

也許這個問題是重複的。但我找不到我的問題的答案。 我得到這個問題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 
} 
?> 

請幫我構造這段代碼。

謝謝。

+0

你爲什麼不使用stmt- $>錯誤顯示數據庫錯誤。 – Deep

回答

1

您需要安裝mysqlnd支持。 (MySQL的本機驅動程序)

0

可以代替你作爲波紋管 我得到的問題,像你這樣的,但是這將解決,請嘗試一下謝謝

public function getAllChatRooms() { 
    $query = "SELECT chat_room_id, name, created_at FROM chat_rooms WHERE 1"; 
    if ($result = mysqli_query($this->conn, $query)) { 
     return $result; 
     mysqli_free_result($result); 
    } 
} 

public function getAllUsers() { 
    $query = "SELECT user_id, name, email, gcm_registration_id, created_at FROM users WHERE 1"; 
    if ($result = mysqli_query($this->conn, $query)) { 
     return $result; 
     mysqli_free_result($result); 
    } 
} 
相關問題