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

I am currently getting this error on one of my files:
/var/www/vhosts/httpdocs/generator/index.php on line 6
Line 6 would be resulting in:
echo $results->GetBookCodesResult->BookCodes->BookInfo->ServiceCode;
My code:
<?php
$config['soap_url'] = "http://myservice.com?WSDL";
if(isset($_REQUEST['codes'])) {
$results = request_Data(explode("\n",$_REQUEST['codes']));
echo $results->GetBookCodesResult->BookCodes->BookInfo->ServiceCode;
}
....
What is the best method to have this fixed? Some advice would be appreciated.

your "->BookCodes" sub-property is empty as you can see in your var_dump-output...
this property "BookInfo" just not exist.. maybe no books are found? ;)

Related

How do I fix a undefined index error in Drupal 7?

Problem
I am getting a "Undefined index: node in include()" notice in Drupal for the below line of code. I've tried the below solution but I am still receiving the error. Any ideas?
Code
$url = drupal_lookup_path('alias', 'node/' . $related['node']->nid);
The solution I tried
isset($related['node']->nid) ? $related['node']->nid : "";
Question
Does anyone know why this error continues to occur?
The error suggests the $related array variable doesn't have a node index.
First, make sure you're retrieving the node object correctly.
Then, perhaps try it like this to avoid errors:
<?php
$url = '';
if (isset($related['node']) && is_object($related['node'])) {
$nid = $related['node']->nid;
$url = drupal_lookup_path('alias', "node/$nid");
}

getQty() giving fatal error in magento 1.9

I am trying to get the collection based on product and quote Id using,
$quotecollection = Mage::getModel('sales/quote_item')->getCollection()->addFieldToFilter('quote_id',$quoteId)->addFieldToFilter('product_id',$product_id);
$quotecollectionaArr = $quotecollection->getData();
echo $quotecollection->getQty();
which gives the following error
Fatal error: Call to undefined method
Mage_Sales_Model_Resource_Quote_Item_Collection::getQty() in
C:\xampp\htdocs....
Please help.
You should use getFirstItem()
$quotecollection = Mage::getModel('sales/quote_item')->getCollection()->addFieldToFilter('quote_id',$quoteId)->addFieldToFilter('product_id',$product_id)->getFirstItem();
echo $quotecollection->getQty();

[PHP]Severity: Compile Error Message: Cannot redeclare <function> - CodeIgniter

I have this function:
function vai_a_capo($cont_sett){ ---> line 3
if($cont_sett >= 7){
echo "</tr><tr>";
return TRUE;
}else{
return FALSE;
}
} ---> line 10
I'm using CodeIgniter version: 3.1.2.
When I go to the link: ../index.php/home/pages/disponibilita it say to me:
Fatal error: Cannot redeclare vai_a_capo() (previously declared in C:\xampp\htdocs\CI___________\application\views\pages\disponibilita.php:3) in C:\xampp\htdocs\CI___________\application\views\pages\disponibilita.php on line 10
Thanks for the answers. Bye.
EDIT:
It has been solved. In my controller (Home) I declared by mistake twice of these:
$this->load->view('pages/disponibilita', $data);
As Paul points out.. yes you could change the function name but since your site is likely riddled with calls to this function, that would likely be a lot of work. Instead, just check to see if the function is already defined by adding 2 lines, like this...
if (!function_exists('vai_a_capo')) { // new line to go above function
function vai_a_capo($cont_sett){ ---> line 3
if($cont_sett >= 7){
echo "</tr><tr>";
return TRUE;
}else{
return FALSE;
}
}
} // new line below function
I should point out, this edit needs to go where your code is trying to define the function a 2nd time.. In \application\views\pages\disponibilita.php on line 10
You should take care of your error messages:
Fatal error: Cannot redeclare vai_a_capo() (previously declared in
C:\xampp\htdocs\CI___________\application\views\pages\disponibilita.php:3) in > > C:\xampp\htdocs\CI___________\application\views\pages\disponibilita.php on line 10
They tell you, that the method you are trying to declare has already been declared in disponibilita.php. You have to give your method another name.

YQL Query Returning Errors

So I have been working on a school project and have gotten this code for a website to work sometimes but other times it returns the error:
Notice: Trying to get property of non-object in C:\xampp\htdocs\schoolproj\getdata.php on line 27
Notice: Trying to get property of non-object in C:\xampp\htdocs\schoolproj\getdata.php on line 27
Ask
Notice: Trying to get property of non-object in C:\xampp\htdocs\schoolproj\getdata.php on line 39
Notice: Trying to get property of non-object in C:\xampp\htdocs\schoolproj\getdata.php on line 39
for the php code:
<html>
<body>
<?php echo $_POST['name']; ?>!<br>
<?php
$endpoint = "http://query.yahooapis.com/v1/public/yql";
$ticker = "'".$_POST["ticker"]."'";
$query = urlencode("env 'store://datatables.org/alltableswithkeys';select * from yahoo.finance.quotes where symbol in (".$ticker.")");
$ch = curl_init($endpoint.'?q='.$query. '&format=json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (curl_error($ch)){
die(curl_error($ch));
}
curl_close($ch);
//echo'<pre>';
$result = json_decode($result);
$symbol = $result->query->results->quote->symbol;
print_r($symbol);
?>
Ask
<?php
$Ask = $result->query->results->quote->Ask;
print_r($Ask);
?>
</body>
</html>
I was wondering if anyone had some advice as to how I could permanently fix the problem or have some sort of error handling. I am new to this so any help would be great. Thanks!
The problematic lines if the code you gave is the whole getdata.php file are:
l.27: $symbol = $result->query->results->quote->symbol;
l.39: $Ask = $result->query->results->quote->Ask;
And the error is telling you that at one point in this your are accessing a property of something that is not an object.
Considering you are saying it sometimes work and sometimes doesn't, it's likely that there are occasional errors, either in your query (depending on your input) or with datatables.org (see this old question on developer.yahoo which indicates that queries would fail when datatables.org doesn't respond).
Then if there is an error, the json you receive will have a structure like below (this is an error I got initially when trying your code because I had forgottent to enclose the ticker in quotes).
{
"error": {
"lang":"en-US",
"description":"Query syntax error(s) [line 1:95 mismatched input 'in' expecting ISNOTNULL]"
}
}
In this result you don't have the query attribute and thus is fails when you try to access it. You should then first check if there is an error (looking for the error attribute), and only if there is none try and access the query results.
To check for the error, you could use something like
if (property_exists($result, "error")) {
// your error handling
} else {
// your current code accessing the results
}

Select data from database and give error

I want get from database table row name but in it i get error. ($query->code_airline => is other query as select from database table row)
code:
<?=$this->db->get_where('ticket_code', array( 'code' => $query->code_airline ))->row()->name?>
error:
A PHP Error was encountered Severity: Notice Message: Trying
to get property of non-object Filename: core/Loader.php(679) :
eval()'d code Line Number: 48
If want use it as:
<?php //This is line 49
$ca = echo $query->code_airline;
$query_tc = $this->db->get_where('ticket_code', array( 'code' => $ca ))->row();
echo $query_tc->name;
?>
have this error:
Parse error: syntax error, unexpected T_ECHO in
D:\xampp\htdocs\system\core\Loader.php(679) : eval()'d code on line 49
How can fix it?
Update:
i use as:
<?php
$ca = $query->code_airline;
$query_tc = $this->db->get_where('ticket_code',array('code'=>$ca));
$row = $query_tc->row(); //This is line 52
echo $row->name;
?>
i have this error with above php code:
A PHP Error was encountered Severity: Notice Message: Trying
to get property of non-object Filename: core/Loader.php(679) :
eval()'d code Line Number: 52
You don't ECHO in a variable assignment
$ca = $query->code_airline; //no echo here!
Also, I believe you're doing something wrong in calling $query->code_airline, but you didn't provide your code so I'm just guessing.
Suggestion: chaining is a nice feature but can be confusing, especially if written as one-liner. First of all, write it in full, you can "compact" your code later if you feel so; keep in mind readability and maintenability also:
$query_tc = $this->db->get_where('ticket_code',array('code'=>$ca));
// $ca is the value assigned above. Make sure you're calling $query->code_airline correctly, as per my suggestion.
$row = $query_tc->row();
echo $row->name;
UPDATE:
you might want to be sure that there are actually any result.
$query_tc = $this->db->get_where('ticket_code',array('code'=>$ca));
// $ca is the value assigned above. Make sure you're calling $query->code_airline correctly, as per my suggestion.
if ($query_tc->num_rows() > 0)
{
$row = $query_tc->row();
echo $row->name;
}
else
{
//do something else
}

Categories