I'm using PHP and CodeIgniter.
I ran a query using the following script:
$query = $this->db->query('select login_id, date_created from prjsite_login');
$row = $query->result();
print_r($row);
The result of the print_r is:
Array ( [0] => stdClass Object ( [login_id] => admin [date_created] =>
2018-04-04 13:18:42 ) )
Which is correct. Thou when I tried to fetch 1 object or value from stdClass using the following script:
echo $query->login_id;
I received an error below:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: pages/home.php
Line Number: 21
Backtrace:
File: C:\xampp\htdocs\BMI_PRJSITE\application\views\pages\home.php
Line: 21 Function: _error_handler
File: C:\xampp\htdocs\BMI_PRJSITE\application\controllers\Pages.php
Line: 11 Function: view
File: C:\xampp\htdocs\BMI_PRJSITE\index.php Line: 315 Function:
require_once
What am I doing wrong?
TIA
You cannot directly get a value from $query because at this point you are just generating a query and you will get the result from $query only after executing it which you are doing at
$row=$query->result();
Looking at your result you are getting a result as an array of a stdClass object so need to json-encode your object and then decode it back to an array
$array = json_decode(json_encode($row), True);
If you are sure you will get only one row then no need for loop and you can simply do it by
echo $array[0]->login_id;
otherwise, you have to go for a loop
foreach ($array as $value) {
echo $value->login_id;
}
As your result is in array and array contain object your have to first access that array then object
echo $row[0]->login_id;
Or use foeach to get all values
foreach($row as $value)
{
echo $value->login_id;
}
Related
I fetched data and fill the value inside the table with for each loop like:
<?php
foreach ($data['rows'] as $value) {
?>
<tr>
<td> <?php echo $value->rsdntname; ?> </td>
<td> <?php echo $value->rsdntemail;?></td>
<td> <?php echo $value->rsdntphone; ?> </td>
</tr>
<?php } ?>
but in top section i have three columns which also filled with the fetch data like:
<h3 class="form-section">Building
<span class="pull-right" style="font-size:15px;color:#000;">
Core <?php $data['rows']->aptcore; ?>,
Floor,
Apartment No
</span>
</h3>
But it is returning me this error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: admin/page.php
Line Number: 52
Why I am getting this error .. Provide me the solution Not able to understand.and give me the solution i am not able to understand .It saying tryng to access non object y this error returning with that. what i am doing wrong there
working with same page to access data for another level with loop its working outside the loop its not working
You're trying to read a property from an object:
$data['rows']->aptcore;
But the error is telling you that it's not an object. So what is it? This previous line of code implies to me that it's probably an array:
foreach ($data['rows'] as $value) {
Because you're looping over the array and reading properties from each object therein.
In a comment elsewhere on this page you specify some debugging output (print_r($data);):
Array (
[res] => 1
[rows] => Array (
[0] => stdClass Object (
[buldname] => BT Tower
[aptno] => 901
[aptcore] => 2
[aptfloor] => 2
[rsdntname] => Gaurav
[rsdntemail] => Gaurav#gmail.com
[rsdntphone] => 9891110987 )
(formatting mine)
If I understand that output correctly, $data is an associative array with two named elements, res and rows. rows is itself also an array containing one object. (Or does it contain more that you're just not showing us?) An array of one object is still an array, not an object. (A basket containing one apple is still a basket, not an apple.)
You could try to index the array to access the first element therein:
$data['rows'][0]->aptcore
Or if there are more elements and you want all of them, you can loop over the array exactly like you already do:
foreach ($data['rows'] as $value) {
echo $value->aptcore;
}
You are calling $value as if it is an object, but more than likely your $value is actually a nested array. Assuming your data in $data['rows'] are arrays then instead of using $value->rsdntname you should call it as $value['rsdntname']... and so on for each $value.
I try to get some information out of my database to my webpage. Everything seems to be fine but there is one thing that doesn't want to go right. I put all my information out my database into $data. When i do this
print_r($data);
My webpage gives me this:
(
[0] => stdClass Object
(
[reparatie_id] => 19
[customer_id] => 4
[medewerker] => 4
[name] => Joost
)
)
Everything seems to be good but when i try to do this:
echo $data->voornaam;
I keep getting this error
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: reparaties/cases.php
Line Number: 7
Backtrace:
File: C:\Ampps\www\beco\application\views\reparaties\cases.php
Line: 7
Function: _error_handler
File: C:\Ampps\www\beco\application\controllers\Reparaties.php
Line: 57
Function: view
File: C:\Ampps\www\beco\public\index.php
Line: 315
Function: require_once
Since your $data is a single-dimensional-array,so it need to be-
$data[0]->reparatie_id;
$data[0]->customer_id;
$data[0]->medewerker;
$data[0]->name;//so on for other indexes
Actually the $data array has an object at 0 position. So you need to any property of object. do like this:
<?php
$data[0]->reparatie_id;
$data[0]->customer_id;
$data[0]->medewerker;
$data[0]->name; ?>
output will be:
19, 4, 4, joost
First of all, your $data array does not have the voornaam element in it. So, assuming you want to echo out the elements that are inside the array, you would use the foreach array, like this:
foreach($data as $value) {
echo $value->name;
echo $value->voorname; //if it exists
}
But, if you just want to access that single element from the array then you would do this:
echo $data[0]->name;
I use this code for Virtuemart:
$product_id_to_remove = 3;
$cart = json_decode($_SESSION['__vm']['vmcart']);
foreach($cart->cartProductsData as $k => $v){
if($v->virtuemart_product_id == $product_id_to_remove) unset($cart->cartProductsData[$k]);
}
$_SESSION['__vm']['vmcart'] = json_encode($cart);
but I get the fatal error: Cannot use object of type stdClass as array in ... line 4. If I add true at json_decode($_SESSION['__vm']['vmcart']) I get the warning: Invalid argument supplied for foreach().
How to resolve the problem?
p.s. I'm beginner in php and don't know json_ at all. The code is suggested by the link: stackoverflow.com/questions/28691203/how-to-remove-a-single-product-from-mod-virtuemart-cart
$cart->cartProductsData behaves like an array but it's actually an object
try this:
change
unset($cart->cartProductsData[$k])
to
unset($cart->cartProductsData->$k)
I am using the following code to get the number of likes on a page.
<?php
$site="http://graph.facebook.com/?ids=http%3a%2f%2fXXXXXXXX/svce.php";
$graph= file_get_contents($site);
$json_string=$graph;
$array = json_decode($json_string, true);
//echo "<pre>";
//print_r($array);
$var = $array['shares'];
echo $var;
?>
But whenever i try to echo out the following code. I always get an unidentified index Notice which is as following: Notice: Undefined index: shares in C:\xampp\htdocs\graphapi.php on line 19
Where am i going wrong?
Here's the print_r output:
Array
(
[http://xxxxxxxxx/svce.php] => Array
(
[id] => http://xxxxxxxxx/svce.php
[shares] => 7
[comments] => 3
)
)
According your print out looks like there is an array more in $array. Try this;
echo $array['http://xxxxxxxxx/svce.php']['shares'];
You have to use the site-name as an key before.
Structure:
- http://example.com
- id
- shares
This means in PHP:
$array["http://example.com/path/to/site"]["shares"];
I have an array $aMethods whose print_r output is this:
Array
(
[0] => Array
(
[pattern] =>
[return_media] => 1
[return_name] =>
)
)
I'm trying to access 'return_media' with this code:
$iReturnMedia = $aMethods[0]->return_media;
echo $iReturnMedia;
Also, when I tried this:
$iReturnMedia = $aMethods[0]['return_media'];
I get an error stating: Cannot use string offset as an array in...
But it's not working, $iReturnMedia comes back as blank. Could someone tell me what I'm doing wrong here?
EDIT: $aMethods is set in a foreach loop as such:
foreach ($aMethodList as $sMethodGroup => $aMethods) { //insert code from above }
You need to use:
$iReturnMedia = $aMethods[0]['return_media'];
The operation -> is for accessing object properties. Since you're just dealing with nested arrays, you need to index them with [].
Access the array value by key.
$iReturnMedia = $aMethods[0]['return_media'];
echo $iReturnMedia;
Your accessing it as if it was an object in an array, you do it like:
$iReturnMedia = $aMethods[0]['return_media'];
echo $iReturnMedia;
Try this,
$iReturnMedia = $aMethodList[$sMethodGroup][0]['return_media'];
echo $iReturnMedia;
Try to var_dump($aMethods) . It will be give exactly idea of that array...
find below the code to access the array values -
foreach ($aMethodList as $sMethodGroup => $aMethods) {
echo $aMethods[0]['return_media'];
}