This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I got this error and cant fix it help me please
Message: Trying to get property of non-object
Filename: controllers/auth.php
Line Number: 20
and this is my code:
The line 20 starts at if ($user->mail){
if ($user->mail) {
$this->session->set_flashdata("Success","You Are now logged in");
$_SESSION['user_logged'] = TRUE;
$_SESSION['username']=$user->username;
}
else {
$this->session->set_flashdata("error", "No Account exists in Database");
}
}
The most likely cause of the error message is that $user is null.
Well as the error is self explanatory, you are using:
$user->mail
$user->username
But according to the error $user is not an object and as such you can not get property of non-object. So try to figure out what exactly are you doing with $user, returning an object or an array.
Related
This question already has an answer here:
Message: Trying to access array offset on value of type null [duplicate]
(1 answer)
Closed 4 months ago.
This is my code:
$evaluationjob = evaluation_elements_jobs::where('job_id', $user->job_id)
->where('company_id',$company_check->id)
->first();
The error in this line:
$items = json_decode($evaluationjob["element_degree"]);
The error message is:
Trying to access array offset on value of type null
You can try this:
$evaluationjob = evaluation_elements_jobs::where('job_id', $user->job_id)
->where('company_id',$company_check->id)
->first();
if ($evaluationjob != null && isset($evaluationjob["element_degree"])) {
$items = json_decode($evaluationjob["element_degree"]);
}
This will check if $evaluationjob is not null and if the value $evaluationjob["element_degree"] exists in the variable.
The error you are having here is the fact that you're trying to access a property on a variable that is null;
So you need to put a check on the variable and the property.
if ($evaluationjob != null && isset($evaluationjob["element_degree"])) {
$items = json_decode($evaluationjob["element_degree"]);
}
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
My goal is set value if field is empty.
But get "Trying to get property of non-object" error.
last_day is empty field, how to assign value 1 ?
public function getPointsForExercises(){
$ls = Auth::user()->lessons()->get();
$last_day = Auth::user()->user_settings->last_day;
if (empty($user->user_settings->last_lesson)) {
$user->user_settings->last_lesson = 1;
}
First problem: $user variable is not defined.
What you are trying to achieve can be done using the exists function to make sure the relation exists, and then update the value using the update function, like this:
public function getPointsForExercises() {
$ls = Auth::user()->lessons()->get();
$last_day = Auth::user()->user_settings->last_day;
if (Auth::user()->user_settings()->exists() && empty(Auth::user()->user_settings->last_lesson)) {
Auth::user()->user_settings()->update([
'last_lesson' => 1
]);
}
The code above is not ending the function nor is it using the variables $ls and $last_day as your code.
You did not set the $user object.
firstly you have to make object :
$user = Auth::user()
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
How to resolve this problem ?
if (strpos($scripts->item($i)->nodeValue, "test") !== false) { //line 127
return true
}
Notice: Trying to get property of non-object on line 127
Check if values exist at all
if(!empty($scripts->item($i)->nodeValue))
{
//your code
}
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
Not sure why I'm getting this error as the code is working and I can see the data I expected.
Code
$allSales = array();
foreach ($game['sales'] as $sale) {
foreach ($sale['values'] as $id => $values) {
$allSales[$id]+=$values['y'];
}
}
Error 1
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: player/game.php
Line Number: 81
Error 2 (Same Code)
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 1
Filename: player/game.php
Line Number: 81
The statement:
$allSales[$id] += $values['y'];
means to take the current value of $allSales[$id], add the value of $values['y'] to it, and store that result back in $allSales[$id]. But the first time that you encounter a particular $id, there is no $allSales[$id]. This results in the warning when you try to get its current value.
Change to:
if (isset($allSales[$id])) {
$allSales[$id] += $values['y'];
} else {
$allSales[$id] = $values['y'];
}
Or you could just prefix the line with # to suppress warnings (I know purists will cry over this):
#$allSales[$id] += $values['y'];
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
Fatal error: Call to a member function result() on a non-object in F:\xampp\htdocs\application\models\mod_contactus.php on line 24
What is this error ? plase help me to figure out.
my model
function message_count(){
$this->db->where('contactus_status', 'no');
$num = $this->db->count_all_results('tbl_contactus');
return $num->result();
}
controller
function message_count()
{
if($num = $this->mod_contactus->message_count())
{
$count['records'] = $num;
}
$this->load->view('admin/admin_messages',$count);
}
view
<?php if(isset($count)){echo $result;}?>
$num = $this->db->count_all_results('tbl_contactus');
return $num->result();
As the function name implies, count_all_results() returns the count of table rows.
So $num is of type integer, therefore not an object so calling $num->result() is wrong (and pointless).
If you want the results, fetch them and count them later. Otherwise just return the count:
return $this->db->count_all_results('tbl_contactus');
Anyway, it's all in the manual, as I suggested in your previous question you should read it thoroughly before hacking code together. Or at least read it when you're stuck. Refernce for AR: http://ellislab.com/codeigniter/user-guide/database/active_record.html. In this case:
$this->db->count_all_results();
Permits you to determine the number of
rows in a particular Active Record query. Queries will accept Active
Record restrictors such as where(), or_where(), like(), or_like(),
etc.