Codeigniter - Trying to get property of non-object - php

Please help with the error I'm getting in Codeigniter and grocery crud. Below code is throwing the following message and I'm stuck with it for a few days, I'm a total noob : (
Error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/examples.php
Line Number: 70
ps:
Function does what it needs, but the above error shows up.
I appreciate your thoughts on this!
function security() {
$method = $this->uri->segment(3); //tell ci that we are working on url segment 3, e.g. delete, update ....
if ($method == "edit" or $method == 'update_validation' or $method == 'delete') {
$id = $this->uri->segment(4); //work on url segment 4, now pointing at posts table primary key
$this->db->where('posts.user_id', $this->session->userdata('id'));
$result = $this->db->get_where('posts', array('id' => $id), 1)->row();
//this is line: 70 if ($result->id != $id)
{
echo "You don't have access";
exit;
}
else return true;
}
}

You may want to add a value check to the $this->db->get_where('posts', array('id' => $id), 1)->row(); line. It seems like that line may be returning false or something if no row is available, though I can't find the exact return value in the documentation.
$result = this->db->get_where('posts', array('id' => $id), 1)->row();
if (!$result || $result->id != $id) {
echo "You don't have access";
exit;
}

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!";
}
}
}

Trying to get property of non-object Notice in custom avatar function for Wordpress

The following custom avatar functions works fine, but I get the following notice
"Trying to get property of non-object in..."
The notice says the problem is on the last part of the function I pasted here - I marked it in the code (look for <-- Notice mentions this line)
Any idea how to fix this? I am stuck...
function test_get_avatar($avatar, $id_or_email, $size, $default, $alt) {
if (!is_numeric($id_or_email)) {
if (is_string($id_or_email)) {
$user = get_user_by('email', $id_or_email);
$id_or_email = $user->ID;
} else if (is_object($id_or_email)) {
if (!empty($id_or_email->ID)) {
$id_or_email = $id_or_email->ID;
}
if (!empty( $id_or_email->comment_author_email)) {
$user = get_user_by('email', $id_or_email->comment_author_email);
$id_or_email = $user->ID; <-- Notice mentions this line
}
}
}
$avatar_id = get_user_meta($id_or_email, 'nicobartes_user_avatar', true);
...
Yes, because get_user_by() can fail and return false. At that point you won't have a wp user object. A test around this code would be:
if ($user = get_user_by('email', $id_or_email->comment_author_email)) {
$id_or_email = $user->ID;
} else {
//Whatever you want to do when this lookup fails
$id_or_email = 0;
}
When you run
$user = get_user_by('email', $id_or_email->comment_author_email);
You should check the value of $user before attempting to get the id on the next line. According to the Wordpress docs, it could potentially be false if no user is found.

Always getting error when cart is empty, Laravel 5

Facing this error:
ErrorException in CartController.php line 35: Trying to get property of non-object
This is the code:
public function index()
{
$this->data['details'] = Cart::content();
$this->data['shipping'] = Shipping::where('region_category_id',session('location'))->where('type',session('type_komoditi'))->first();
$regType = session('regType');
$regId = session('id_wilayah');
$qReg = RegionCategory::find($regId);
if($regType == 'children') {
$this->data['minimalWeight'] = $qReg->minimal_weight;
//$this->data['minimalBuy'] = $qReg->min_buy;
}
$this->data['minimalBuy'] = $qReg->min_buy; //this is line 35
$this->data['regType'] = $regType;
\Session::put('price',Cart::total());
\Session::put('totalPrice',Cart::total());
\Session::put('paycode',0);
return view('client.carts.index',$this->data);
}
when I delete the line 35 the error is gone, but that code is important for using minimal buy filter.
How to resolve this error?
That's error cause $qReg->min_buy return null.
You can fix by return default value like this:
$this->data['minimalBuy'] = $qReg->min_buy ?? 0;
That's mean if is null return 0
It's saying that $qReg is not an object and you are trying to get the value from it using min_buy property.
Try printing the object $qReg using print_r($qReg); and exit the script after that.
print_r($qReg);
exit();
And check whether that object has the property min_buy or not.
Why is $this->data['minimalBuy'] = $qReg->min_buy; after if block ? It's being overwritten even if if($regType == 'children') is false.

Laravel 5.3 Check if result exist

I'm having trouble getting this thing to work. Basically this function is called once I click the delete button of an user config. Users can have multiple configs. What happens is that once I delete a config it looks for another one, if found it sets that as the current profile. If not it should set the config_done back to 0. the first bit works fine but if $firstProfile returns nothing, instead of entering the else it returns me an error..
code:
public function delete(UserConfig $config) {
$user = Auth::user();
$uid = $user->id;
if ($uid == $config->user_id) {
UserConfig::where('id', $config->id)->delete();
$firstProfile = UserConfig::where('user_id', $uid)->first()->id;
if(count($firstProfile)){
$user = User::find($uid);
$user->current_profile = $firstProfile;
$user->save();
return view('/settings');
} else {
$user = User::find($uid);
$user->config_done = 0;
$user->save();
return view('/home');
}
}
}
error:
ErrorException in UserConfigController.php line 100:
Trying to get property of non-object
in UserConfigController.php line 100
at HandleExceptions->handleError('8', 'Trying to get property of non-object', '/Users/jordykoppen/git/beef-monitor/app/Http/Controllers/UserConfigController.php', '100', array('config' => object(UserConfig), 'user' => object(User), 'uid' => '1')) in UserConfigController.php line 100
Keep in mind that I'm very new to Laravel and the whole MVC environment.
Eloquent will return null if no result is found. You're dealing with objects here, so rather than checking the count, you can just do this.
$firstProfile = UserConfig::where('user_id', $uid)->first();
if($firstProfile){
// success
} else {
// not result
}
The $firstProfile variable will either be an instance of UserConfig or it will be null, no need to check the count.

CodeIgniter - checking data and receiving 'Trying to get property of non-object'

Im trying to test a pin number in the database compared to one entered by a user, whenever i run it i get a 'Trying to get property on non-object' error ... I cant seem to spot where im going wrong, could someone please help me out ... Its saying the error is on the $thepin = $pins->pin; line
The code i have in my controller is as follows:
function check_pin()
{
$pin = md5($this->input->post('oldpin'));
$email = $this->input->post('email');
$existingpin = $this->users->get_pin_by_email($email);
foreach($existingpin as $pins){
$thepin = $pins->pin;
}
if($pin != $thepin){
$this->form_validation->set_message('check_pin', 'The Old Pin Number does not match the existing one');
return FALSE;
} else {
return TRUE;
}
}
and the following is the code in my model
function get_pin_by_email($emailaddress)
{
$this->db->where('LOWER(email)=', strtolower($emailaddress));
$query = $this->db->get($this->table_name);
if ($query->num_rows() == 1) return $query->row();
return NULL;
}
The Controller code should be:-
function check_pin()
{
$pin = md5($this->input->post('oldpin'));
$email = $this->input->post('email');
$existingpin = $this->users->get_pin_by_email($email);
foreach($existingpin as $pins){
// Check for the NULL return
if ($pins != NULL && is_object($pins)) {
$thepin = $pins->pin;
}
}
if($pin != $thepin){
$this->form_validation->set_message('check_pin', 'The Old Pin Number does not match the existing one');
return FALSE;
} else {
return TRUE;
}
}
If you look closely in the model code, you will find that you are returning two data types - one is Object & another is NULL. So in your controller code, you should also check that accordingly.
Hope it helps.

Categories