Call to a member function get() on a non-object - php

I'm using the Codeigniter Framework and I'm getting the following error,
call to a member function get() on a non-object in site_model.php on line 11
which is the row with the get() . test is the name of my table in my database. What am i doing wrong?
<?php
class Site extends CI_Controller
{
function index()
{
$this->load->model('site_model');
$data['records']= $this->site_model->getAll();
$this->load->view('home',$data);
}
}
?>
<?php
class Site_model extends CI_Model
{
function getAll()
{
$q = $this->db->get('test');
if($q->num_rows > 0 )
{
foreach ($q ->result() as $row )
{
$data[] = $row ;
}
}
return $data;
}
}

My best guess is you didn't setup your database:
When a model is loaded it does NOT connect automatically to your
database.
See the guide here: https://www.codeigniter.com/user_guide/general/models.html#connecting-to-your-database

Related

Invalid argument supplied for foreach() - Codeignitor

I am getting an error "Invalid argument supplied for foreach()" while trying to fetch 'id' column using foreach loop:
Codeignitor version: 3.0.1
Controller:
<?php
class Users extends CI_Controller {
public function show(){
$this->load->model('user_model');
$result = $this->user_model->get_users();
foreach($result as $object){
echo $object->id;
}
}
}
?>
Model:
<?php
class User_model extends CI_Model {
public function get_users(){
$this->db->get('users');
}
}
?>
As I checked table name and column names are correct.
<?php
class User_model extends CI_Model {
public function get_users(){
$data=$this->db->get('users');
return $data->result();
}
}
?>
you can fetch by using either result() or result_array() in model
in your case
public function get_users(){ // model
$data=$this->db->get('users');
return $data->result();
}
public function show(){ // controller
$this->load->model('user_model');
$result = $this->user_model->get_users();
foreach($result as $object){
echo $object->id;
}
}
The above method returns the query result as an array of objects, or an empty array on failure.
The below one method returns the query result as a pure array, or an empty array when no result is produced.
you can also do this
public function get_users(){ // model
$data=$this->db->get('users');
return $data->result_array();
}
public function show(){ // controller
$this->load->model('user_model');
$result = $this->user_model->get_users();
foreach($result as $object){
echo $object['id'];
}
}
for more details click here
you need to return result() or result_array()
<?php
class User_model extends CI_Model {
public function get_users(){
$data=$this->db->get('users');
return $data->result(); // or return $data->result_array();
}
}
?>
for result arrray you need to echo $object['id'];
and for result() same as you printing.

How Select and view count rows from database with Codeigniter

This is my controller : Admin
class Admin extends CI_Controller {
public function index()
{
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi()->result();
$this->load->view('Admin_view',$data);
}
}
This my model : Dash_model
public function jml_instansi()
{
$query = $this->db->query("SELECT * FROM instansi");
return $query->num_rows();
}}
This my view : Admin_view
<?php echo $jumlah_instansi; ?>
Please help me, sorry newbie..Thank you..
Thats show error
Message: Undefined property: Admin::$Dash_model
Filename: controllers/Admin.php
and
Message: Call to a member function jml_instansi() on a non-object
You must first load a model before accessing it, e.g.
public function index()
{
$this->load->model('Dash_model');
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi()->result();
// ...
}
See Loading a Model for more.
Try like this.You are returning integer value of count so no need to use result() result set in controller.
Controller:
class Admin extends CI_Controller {
public function index()
{
$this->load->model('Dash_model');//load model
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi();
$this->load->view('Admin_view',$data);
}
}
Model
public function jml_instansi()
{
$query = $this->db->query("SELECT * FROM instansi");
return $query->num_rows();
}}
View:
<?php echo $jumlah_instansi; ?>

Fatal error: Call to a member function where() on a non-object

i got this fatal error when i run this on browser please solve my problem...i don't get whats wrong with this code.........Fatal error: Call to a member function where() on a non-object
Fatal error: Call to a member function where() on a non-object
model
<?php
class News extends CI_Model
{
function Users(){
//call model constructor
parent::Model();
//load database
$this->load->database();
}
public function get_all_news(){
$this->db->get('news');
if($query->num_rows()>0){
//return result set as associate array
return $query->result_array();
}
}
public function getnews($field,$param){
$this->db->where($field,$param);
$query=$this->db->get('news');
// return result set as accosiate array
return $query->result_array();
}
public function getnumnews(){
return $this->db->count_all('news');
}
}
?>
controller
<?php
class News_data extends CI_Controller{
public function Users(){
// load controller parent
parent::Controller();
// load ‘Users’ model
}
public function index(){
$this->load->model('News');
$data['users']=$this->News->getnews('id <',5);
$data['numusers']=$this->News->getnumnews();
$data['title']='Displaying user data';
$data['header']='User List';
// load ‘Show_data’ view
$data['title']='Display data';
$this->load->view('Show_data',$data);
}
}
?>
view
</head>
<body>
<h1><?php echo $header;?></h1>
<ul>
<?php foreach($users as $user):?>
<li>
<p><?php echo $user['id'].' '.$user['title'].' '.$user['tetxt'];?></p>
</li>
<?php endforeach;?>
</ul>
<p><?php echo 'Total number of users :'.$numusers;?></p>
</body>
</html>
?>
You may not initialising the db object $this->db here $this->db->where($field,$param); OR your $this->db is null.
Please load the database in the model constructor or load via autoload.php
please modify your model as shown below
class News extends CI_Model
{
function __construct(){
$this->load->database();
}
function Users(){
//call model constructor
parent::Model();
//load database
$this->load->database();
}
public function get_all_news(){
$this->db->get('news');
if($query->num_rows()>0){
//return result set as associate array
return $query->result_array();
}
}

Call to a member function where() on a non-object in ... on line 13

I am new user of CodeIgniter, I am trying to create simple login page using CodeIgniter but I am receiving the following error:
Call to a member function where() on a non-object in C:\xampp\htdocs\CodeIg\application\models\user.php on line 13
I am uncertain as to where to begin debugging this, suggestions would be appreciated.
My code for the model is as follows:
<?php
class User extends CI_model{
function __construct()
{
parent::__construct();
}
public function verifyuser()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$remember = $this->input->post('remember');
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('user_login');
$result = array();
if($query->num_rows==0)
{
$result['false']=false;
return $result;
}
else
{
$result=$query->result();
foreach($result as $item)
{
$result['id']=$item->id;
$result['username']=$item->username;
$result['password']=$item->password;
}
return $result;
}
}
}
?>
and code for controller is:
<?php
class User_Controller extends CI_controller
{
public function getloginData()
{
$this->load->model('User');
$rvalue = $this->User->verifyuser();
header('Content-type: application/json');
echo json_encode($rvalue);
}
}
?>
Database is not initialized.
You need to include the "database" in your application/config/autoload.php:
$autoload['libraries'] = array('database', 'session');
Or in your model class constructor:
class User extends CI_model{
public function __construct()
{
parent::__construct();
$this->load->database();
}
}
You can get more information here: http://ellislab.com/codeigniter/user_guide/database/connecting.html
$this->db isn't initialized, so $this->db is null and calling where() on null results in this error.. you have to assign something to $this->db first.
To do this, you have to laod the Model within your controller

Why does this model return "non-object error", when it worked in the controller?

In my controller I went from getting the data right there (which worked fine):
$data['query'] = $this->db->query("MY DB QUERY");
$this->load->view('shopci_view', $data);
to grabbing the data from a class model function:
class Shopci_model extends CI_Controller {
function __construct()
{
parent::__construct(); //model constructor
}
//display sale itmes on shopci_view.php
function sale_items()
{
$query = $this->db->query('MY DB QUERY - SAME AS ABOVE');
return $query->result();
}
}
new controller function:
//load model, auto connect to db
$this->load->model('Shopci_model', '', TRUE);
//Display items for sale
$data['query'] = $this->Shopci_model->sale_items();
$this->load->view('shopci_view', $data);
ERROR:
Fatal error: Call to a member function result() on a non-object in
shopci_view
This is the line in the view that worked before I switched to the model (didn't change view):
<?php foreach($query->result() as $row): ?>
Any help is appreciated.
Thank You.
In your model you return the $query as a result() array to the controller.
So you cannot then use 'foreach $query->result()' again - because you have already done that;
This should work
<?php foreach($query as $row): ?>
OR if you want - just change the model from
return $query->result();
to
return $query;
In your model you want to return the data to pass to the controller. So in your model you would have
function sale_items()
{
$query = $this->db->query(SAME AS OLD);
return $query;
}
and then your controller will be the same and your foreach in your view should be the following
<?php foreach ($query as $row): ?>

Categories