Reputation: 606
I am very new to CodeIgniter. I am getting below error. Can someone please help me out. I checked various answers on StackOverflow but none helped me.
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
Below is the controller code
<?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);
}
}
?>
And this is the view code
<!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>
Currently not using the data coming from controller. Any help will be highly appreciated. Thanks in advance.
Upvotes: 1
Views: 14700
Reputation: 381
In your case you can use:
$query = $this->db->get("stud")->result();
or
$query = $this->db->get("stud")->result_array();
To access the data you can put the $query in a foreach like the examples below and access it as an object or as an array.
You have two kind of approaches:
result()
This method returns the query result as an array of objects, or an empty array on failure.
Example:
$query = $this->db->query("YOUR QUERY");
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
or result_array()
This method returns the query result as a pure array, or an empty array when no result is produced.
Example:
$query = $this->db->query("YOUR QUERY");
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
Upvotes: 0
Reputation: 1192
You should use something like this
$data['records'] = $query -> result_array();
Upvotes: 5