Trying to get property of non-object/ Php codeigniter - php

I can't seem to figure about what is wrong with this code. thank yu for your help.
I am getting error messages:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/members.php
Line Number: 24
My model:
public function show_user()
{
$this->db->select('');
$this->db->from('users');
$this->db->where('email',$this->input->post('email'));
$q=$this->db->get('');
if($q->num_rows() > 0 )
{
$data = array();
foreach($q->result() as $row)
{
$data=$row;
}
return $data;
}
}
My controller:
public function members()
{
if ($this->session->userdata('is_logged_in'))
{
$this->load->model('model_users');
$data['member'] = $this->model_users->show_user();
$this->load->view('members', $data);
}
else
{
redirect('main/restricted');
}
}
My view:
echo "<p> Congratulations you are logged in!!</p>";
foreach($member as $row)
{
echo $row->Name. "<br/>";
echo $row->email. "<br/>";
}
So this my code. I want to display user information from database but I am getting errors. please help me..

You only need to return $q-result();
if($q->num_rows() > 0 )
{
return $q-result();
}

I'm not sure what your database looks like but I suspect this is because either the Name or email properties don't exist on the $row object. Are you sure your field in your users table is called Name or is it name (lowercase)? If its lowercase your code should be.
foreach($member as $row)
{
echo $row->name. "<br/>"; //Changed this line
echo $row->email. "<br/>";
}
Also your foreach loop on your model is a bit pointless. You might as well remove it and just do
return $data->row;

Related

How to solve this CodeIgniter error message? "Notice: Trying to get property of non-object in CodeIgniter"

I am testing the app I created for possible potential bugs and fixing some security holes into it. It came across my mind to check the database and table of the app exist and if it does it runs the code and if the tables does not exist, it will echo a warning message. However, whenever I am testing a feature, I am getting an error saying, Trying to get property 'project_name' of non-object. Here is the code in my model to which I am testing if the table and column exist:
public function get_project($id){
if ($this->db->field_exists('id','projects') == FALSE) {
$this->db->where('id', $id);
$query = $this->db->get('projects');
if ($query->num_rows() > 0) {
return $query->row();
}return false;
}else{
echo "Table and column does not exist!";
}
}
As you can see, the line where in I am testing if the field exist where the value is FALSE in order for me to display the error warning in the else statement. However, whenever I am doing this, I am getting an error saying Trying to get property 'project_name' of non-object in my views. Here is the code in my view:
<h3>Project Name:<?php echo $project_data->project_name; ?></h3>
<h3>Date Created:<?php echo $project_data->date_created; ?><h3>
<h3>Description:</h3>
<p class='projects-description'>
<?php echo $project_data->project_body; ?>
</p>
you have the wrong testing for the field if it is exist or not,
please change this line from:
if ($this->db->field_exists('id','projects') == FALSE) {
To
if ($this->db->field_exists('id','projects') != FALSE) {
then you will be good to go!
Edit, Surround the above code inside
if ($this->db->table_exists('yourtablename') )
{
// table exists
}
else
{
// table does not exist
}
you have wrong if statement it is true or blank,
please change this line :
if ($this->db->field_exists('id','projects')==FALSE) {
use this line
if ($this->db->field_exists('id','projects')) {
here ..
public function get_project($id){
if ($this->db->field_exists('id','projects')) { //its already check true
$this->db->where('id', $id);
$query = $this->db->get('projects');
if ($query->num_rows() > 0) {
return $query->row();
}return false;
}else{
echo "Table and column does not exist!";
}
}
}

Error Trying to get property of non-object and Undefined variable: Codeigniter

I am facing the following error:
Trying to get property of non-object and Undefined variable php
errors in my code
Controller:
function showDoctorInformation(){
$this->load->model('PatientModel');
$data['doctorinfo'] = $this->PatientModel->getDoctorInformation();
$this->parser->parse('patient_msgview', $data);
}
Model:
function getDoctorId() {
$this->db->from('person');
$this->db->select('doctorId');
$doctorId = $this->db->get()->result();
return $doctorId;
}
function getDoctorInformation() {
$doctorId = $this->getDoctorId();
$this->db->from('DoctorInfo');
$this->db->where('doctorId', $doctorId);
$this->db->select('name', 'surname', 'Bio', 'Address', 'img');
$doctorinfo = $this->db->get()->result();
return $doctorinfo;
}
View:
<?= $doctorinfo->name ?>
I have displayed information from the database before with this method and I can't see the error now.
result() return
This method returns the query result as an array of objects, or an
empty array on failure
So you need to fetch single data form your database using ->row()
function getDoctorId() {
$this->db->select('doctorId');
$this->db->from('person');
$this->db->select('doctorId');
$query = $this->db->get();
if ($query->num_rows == 1) {
$row=$query->row();// fetch single row
return $row->doctorId;// get doctor id
} else {
return FALSE;
}
}
And in viwe you have to get your data using foreach loop
For exm
foreach ($doctorinfo as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}

codeigniter error with selecting data from table

I cant figure out whats wrong with my model, it said a Fatal Error occured.
Here's my model file:
function getName($no_ktp){
$this->db->select('nama')->from('dt_prbd')->where('no_ktp', $no_ktp);
$qry_getName = $this->db->get();
if ($qry_getName->num_rows() > 0) {
foreach ($qry_getName->result() as $data_getName){
$hasil_qry_getName[] = $data_getName;
}
return $hasil_qry_getName;
}
}
I got this error.
Fatal error: Call to a member function num_rows() on boolean in C:\xampp\[APP_PATH]\M_hrd_apps.php on line 25
I thought the error is in the query, so I changed it into this:
$qry_getName =
$this->db->select('nama')
->from('dt_prbd')
->where('no_ktp', $no_ktp)
->get();
but the error is the same,
Call to a member function num_rows() on boolean
Can anyone help me please?
Tr this
function getName($no_ktp){
$this->db->select('nama')->from('dt_prbd')->where('no_ktp', $no_ktp);
$qry_getName = $this->db->get();
$result = $qry_getName->result();
return $result;
}
}
Try this
In Model
function getName($no_ktp)
{
$query = $this->db->query("SELECT nama FROM dt_prbd WHERE no_ktp= '$no_ktp'");
$result = $query->result_array();
$count = count($result);
if (empty($count))
{
$log = 0;
return $log;
}
else
{
return $result;
}
}
In controller
$result = $this->Model_name->getName();
if($result == 0)
{
echo 'Data Is empty';
}
else
{
$data['name'] = $result;
#rest of your code
}
EDIT 01
To configure your database
go to config/database.php in bottom of page define
Databse name
Mysql Username
Mysql Password
all problem solved, there is no error in code. i try to re-create the db, and then all script turn out like what i expect.
thanks for all help Deep Parekh and Abdulla

Codeigniter: Sum contents of the records in a field

I am fetching some information from mysql database and I am displaying it using the following script(I am using Codeigniter).
Models
public function sum($field, $table, $userfield, $username)
{
return $this->db->query("SELECT SUM($field) FROM $table WHERE $userfield = '$username'");
}
Controller
public function save(){
$data = array(
'sum_simpan' => $this->user_m->sum('besar_simpanan', 'simpanan', 'id', $this->session->userdata('username'))->result()
); }
View
<div class="info-a">
<p>Total Simpanan Anda</p>
<h3><?php
foreach ($sum_simpan as $row) {
echo $row->besar_simpanan;
}
?>
</h3>
</div>
I want to do is sum the contents of a field, and display it in the view.
However I get an error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$besar_simpanan
Filename: anggota/simpanan.php
Line Number: 14
Would you please kindly show me how to do it.
Thanks in Advance :)
Change your model like this
public function sum($field, $table, $userfield, $username)
{
return $this->db->query("SELECT SUM($field) as total FROM $table WHERE $userfield = '$username'");
}
also the view
<h3>
<?php
foreach ($sum_simpan as $row) {
echo $row->total;
}
?>
</h3>

message undefined variable: news on view page

I'm new to PHP and CodeIgniter, and saw that there is many questions mentioning this and been trying them all but nothing seems to work. Everything is auto-loaded in configuration, the database is running and function for posting to database are working but writing to view page doesn't work at all. Except for displaying username, but for that I create a new variable on view page.
Controller
public function ShowNews()
{
$data = array();
$this->load->model('user');
$data['news'] = $this->user->getNews();
$this->load->vars($data);
}
Model
function getNews(){
$q = $this->db->get('News');
if($q->num_rows() > 0){
return $q->result();
}
return FALSE;
}
View
<?php foreach($news as $row) : ?>
<li><?php echo $row->Title; ?> </li>
<li><?php echo $row->Date; ?></li>
<?php endforeach; ?>
This is the error EDIT ves to news
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: admin/Pocetna.php
Line Number: 64
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: admin/Pocetna.php
Line Number: 64
Using WAMP, NetBeans with CodeIgniter. I saw it has to be something with passing data from controller to view but I can't figure it out and been trying few days already, but always having problems.
You're not passing $data to your view. Your controller should be like this:
public function show_news()
{
$this->load->model('user');
$data = [];
$data['news'] = $this->user->get_news();
$this->load->view('news', $data);
}
Your view should also be checking if $news is FALSE, because you'll have some issues with foreach if you loop over the value FALSE. Your model should also be returning result_array not result, foreach cam't loop over objects..
public function get_news()
{
$q = $this->db->get('News');
return($q->num_rows() > 0) ? $q->result_array() : FALSE;
}
Your view should look something like this:
<?php
if($news !== FALSE)
{
foreach($news as $row)
{
echo "<li>{$row['title']}</li>";
echo "<li>{$row['date']}</li>";
}
}
else
{
echo "No news to see here!";
}
?>
Your title also doesn't link up with the error in the post, so that's the solution to the one in the title.

Categories