簡體   English   中英

調用未定義的方法CI_DB_mysqli_result :: results()

[英]Call to undefined method CI_DB_mysqli_result::results()

我是CodeIgniter的新手。 我正在錯誤以下。 有人可以幫我嗎。 我檢查了StackOverflow上的各種答案,但沒有一個幫助我。

An uncaught Exception was encountered

Type: Error

Message: Call to undefined method CI_DB_mysqli_result::results()

Filename: /Applications/XAMPP/xamppfiles/htdocs/CodeIgniter/application/controllers/Stud_controller.php

Line Number: 10

Backtrace:

File: /Applications/XAMPP/xamppfiles/htdocs/codeigniter/index.php
Line: 315
Function: require_once

下面是控制器代碼

<?php
Class Stud_controller extends CI_Controller{
    function __construct(){
        parent::__construct();
        $this->load->database();
    }

    public function index(){
        $query = $this->db->get("stud");
        $data['records'] = $query -> results();
        $this->load->view('Stud_view',$data);
    }
}
?>

這是視圖代碼

<!DOCTYPE html> 
<html lang = "en">

   <head> 
      <meta charset = "utf-8"> 
      <title>Students Example</title> 
   </head>
   <body>
        <a href = "<?php echo base_url(); ?>
         index.php/stud/add_view">Add</a>
   </body>

</html>

當前不使用來自控制器的數據。 任何幫助將不勝感激。 提前致謝。

你應該用這樣的東西

$data['records'] = $query -> result_array();

您可以使用:

$query = $this->db->get("stud")->result();

要么

$query = $this->db->get("stud")->result_array();

要訪問數據,您可以像下面的示例一樣將$ query放在foreach中,然后將其作為對象或數組進行訪問。

您有兩種方法:

結果()

此方法以對象數組或失敗時為空數組的形式返回查詢結果。

例:

$query = $this->db->query("YOUR QUERY");

foreach ($query->result() as $row)
{
        echo $row->title;
        echo $row->name;
        echo $row->body;
}

result_array()

此方法以純數組形式返回查詢結果,如果不生成任何結果,則返回空數組。

例:

$query = $this->db->query("YOUR QUERY");

foreach ($query->result_array() as $row)
{
        echo $row['title'];
        echo $row['name'];
        echo $row['body'];
}

生成查詢結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM