I'm trying to find out why I'm getting a Trying to get property of non-object. I'm not completely skilled with objects and arrays but i'm trying. Here's the code and the error message. Any ideas on how to fix this issue?
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/sitemodel.php
Line Number: 208 and 215
function getSubMenuPages()
{
$this->db->select('site_menu_structures.id');
$this->db->from('site_menu_structures');
$this->db->where('site_menu_structures.short_name', 'mainnav');
$query = $this->db->get();
$menu_id = $query->row()->id;
$this->db->select('site_menu_structures_links.id, site_menu_structures_links.short_name, is_category');
$this->db->from('site_menu_structures_links');
$this->db->where('site_menu_structures_links.menu_structure_id', $menu_id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$linksArray = $query->result();
foreach ($linksArray as $key => $link)
{
if ($link->is_category == 'Yes')
{
$linksArray->{$key}->child_links;
$this->db->select('site_menu_structures_links_children.link_name');
$this->db->from('site_menu_structures_links_children');
$this->db->where('site_menu_structures_links_children.site_menu_structures_links_id', $link->id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$linksArray->{$key}->child_links = $query->result();
}
}
}
}
return $linksArray;
}
My guess would be $linksArray is an array, not an object so the line
$linksArray->{$key}->child_links;
will not work. In any case, this line does nothing so why have it at all?
Where you assign a value to this "property", try this instead
$linksArray[$key]->child_links = $query->result();
"Trying to get property of non-object"
this type of errors only exist if you try to treat a variable as an object instance and you actually failed to create that instance successfully try to check this part of the code if this is really an object or not:
$linksArray->{$key}->child_links
this is on the bottom of your code.
Related
I am using codeigniter and I have made a function to return name of the user. Here is the code:
$this->db->where('u_id', $id);
$query = $this->db->get('users');
$data = $query->result_array();
$string = $data->u_name.' '.$data->u_surname;
return $string;
When I am using this function i get this error:
Message: Trying to get property of non-object and recall the line with
$string = [...]
Use foreach loop:
foreach ($data as $row)
{
echo $string = $data['u_name'].' '.$data['u_surname'];
}
You can try like this.In codeigniter,The row() returns the matched first row according to your condition in object form.
$this->db->where('u_id', $id);
$query = $this->db->get('users');
$data = $query->row();//change here
$string = $data->u_name.' '.$data->u_surname;
//echo $string;
return $string;
for more see Codeigniter Result Sets
Try this code to return
$this->db->select('*');
$this->db->from('users');
$this->db->where(array('u_id'=>$id));
$query=$this->db->get();
if($query->num_rows()()>0)
{
$data = $query->result();
return $data->u_name.' '.$data->u_surname;
}
This is because the function you chose to generate results for you query.
$data = $query->result_array();
result_array() returns the results in an array even if you only had one result, so when you try to $data->u_name for instance, you have this error because the array does not have that property.
You can do a foreach on $data and perform your logic or use other methods to get the result from the query like $query->row_array();
Take a look on the codeigniter documentation: Generating Query Results
So, I have following php for wp:
$usersNames = array();
foreach ($userIDs as $userId) {
$userInfo = get_userdata($userId);
$usersNames[] = $userInfo->display_name; //this one
}
I am getting an error for $usersNames.
"PHP Notice: Trying to get property of non-object in /functions.php on line"
What is going on?
Any suggestions?
Thanks
EDIT:
So, for $userIDs, I have an array of user ids. Then I am trying to get user display name etc for individual ids.
Your function get_userdata() will return False on failure, WP_User object on success.
And error Trying to get property of non-object means that this function has returned false.
Simply check if $userInfo is not empty
$userInfo = get_userdata($userId);
if (!empty($userInfo)) {
$usersNames[] = $userInfo->display_name;
}
you need judge whether $userInfo return the right obj
$usersNames = array();
foreach ($userIDs as $userId) {
$userInfo = get_userdata($userId);
$usersNames[] = isset($userInfo->display_name)?$userInfo->display_name:''; //this one
}
i create my own function with count of data.
Here is my function.
function Test($data){
global $acc;
$count = $acc->get_var("SELECT Count(*) FROM _users WHERE ID='$data'");
if($count == 0)
{
return true;
}else{
return false;
}
}
But i got This is error : "PHP Catchable fatal error: Object of class stdClass could not be converted to string
In This is Line $count = $acc->get_var("SELECT Count(*) FROM _users WHERE ID='$data'");
I try var_dump in function and Other ezSQL methods (query,num_rows) but i got every same error.
--
Edit:
Problem solved. I wrong posted $data
Please make sure that
$data
is not an array
its better practice to pass $acc by reference
function Test($data, &$acc){
$count = $acc->query("SELECT Count(*) FROM _users WHERE ID='$data'");
if($count == 0)
{
return true;
}else{
return false;
}
}
I would like to retrieve the field names in the database, one of which named 'wardno/houseno'. When i tried to retrieve it as object using foreach i'm getting
My code,
$this->db->select('*');
$this->db->from('tbl_application');
$this->db->where('app_no', '1/ML/201314/TVM');
$q=$this->db->get();
foreach($q->result() as $row)
echo $row->wardno/houseno;
The error shows,
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$wardno
Filename: controllers/admin_sona.php
Line Number: 19
A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant houseno - assumed 'houseno'
Filename: controllers/admin_sona.php
Line Number: 19
A PHP Error was encountered
Severity: Warning
Message: Division by zero
Filename: controllers/admin_sona.php
Line Number: 19
Is it possible to retrieve the database value with symbol as object?
Can you please try to give
foreach($q->result_array() as $row)
echo $row['wardno/houseno'];// or even echo $row['wardno\/houseno'];
instead of
foreach($q->result() as $row)
echo $row->wardno/houseno;
Give it a try!
----EDIT------
As you dont want it to be converted into an array, try
echo $row[17]; //Just try. I am not sure where it hurts the code. In case of error, let me know in comments
try to change this:
$row->wardno
to this:
$row['wardno']
Same thing for houseno
$row['houseno']
don't know if you have afield named wardno/houseno inside your database, I thinked that you have two different fields. If is the same field I advise you to use _ instead of / inside your database.
If is the same field try this:
echo $row['wardno/houseno'];
to convert array to object try this:
function arrayToObject($array) {
if (!is_array($array)) {
return $array;
}
$object = new stdClass();
if (is_array($array) && count($array) > 0) {
foreach ($array as $name=>$value) {
$name = strtolower(trim($name));
if (!empty($name)) {
$object->$name = arrayToObject($value);
}
}
return $object;
}
else {
return FALSE;
}
}
$arr = arrayToObject($your_array);
I am using codeigniter for my project and couldn't find what's wrong with my code. This is my controller code
$products = $this->products_model->getProductsQuantity($id);
$q = $warehouse_product->quantity;
object notation is working but getting error: Trying to get property of non-object
$q = $warehouse_product['quantity'];
array notation not working and getting php error: Fatal error: Cannot use object of type stdClass as array in products\controllers\products.php on line xx
Here is my model function
public function getProductsQuantity($product_id)
{
$q = $this->db->get_where('products', array('product_id' => $product_id), 1);
if( $q->num_rows() > 0 )
{
return $q->row();
}
return FALSE;
}
Please help me to fins where is the problem in my code.
You should look at this
if you are limiting your results to 1 as in your get_where instruction then return
$q->row_array() // for array
And
$q->result_array() // for object
And in Controller do this
$products = $this->products_model->getProductsQuantity($id);
$quantity = $products->quantity; // Object notation
And
$quantity = $products['quantity']; // Array notation