Select data from database and give error - php

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
}

Related

Undefinet offset error

My code sometimes give code undefinet offset error.
Error:
[03-Sep-2015 13:06:44] NOTICE: "Undefined offset: 6"
File: /home/mdmeds/public_html/includes/pages/game/class.ShowBuildingsPage.php | Line: 111
and
[04-Sep-2015 17:38:57] NOTICE: "Undefined offset: 8"
File: /home/mdmeds/public_html/includes/pages/game/class.ShowBuildingsPage.php | Line: 111
This is the part of the code
$Element = $CurrentQueue[$QueueID - 2][0]; /**this give the error*/
$BuildEndTime = $CurrentQueue[$QueueID - 2][3];
unset($CurrentQueue[$QueueID - 1]);
$NewQueueArray = array();
foreach($CurrentQueue as $ID => $ListIDArray)
{
if ($ID < $QueueID - 1) {
$NewQueueArray[] = $ListIDArray;
} else {
if($Element == $ListIDArray[0] || empty($ListIDArray[0]))
continue;
$BuildEndTime += BuildFunctions::getBuildingTime($USER, $PLANET, $ListIDArray[0]);
$ListIDArray[3] = $BuildEndTime;
$NewQueueArray[] = $ListIDArray;
}
}
I read lot of articles about this kind of errors but i do not know how to fix my code. Can someone help me please ?
You are trying to play with indexes that you are not sure they even exist...
things like
$CurrentQueue[$QueueID - 2]
is a guess... Get to find another way..
In this piece of code $CurrentQueue[$QueueID - 2][0], the key is generated dynamically based on $QueueID. WHen you get the error, it means that the specified key is not available in the array $CurrentQueue.
To avoid such run time exceptions, you can do something like this
if (!empty($CurrentQueue[$QueueID - 2])) {
// the actual functionality goes here.
}

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
}

Warning: Invalid argument supplied for foreach() in /home/content/03/11247403/html/wp-content/themes/kerdowney-2810/t_destinations.php on line 183

i'm working on a WordPress site, i keep on getting this warning. i thought it has something to do with my magic fields maybe i named something wrong but its still giving me the same error.
How do i solve this problem.
I have this piece of code on t_destinations.php
$ras = getGroupOrder('recommended_accommodation_title' , $ras);
foreach ($ras as $ra) :
$ras_title = get('recommended_accommodation_title', $ras);
$ra_link = get('recommended_accommodation_link', $ra);
$link = !empty($ra_link) ? $ra_link : '#';

FACEBOOK API CALL - PHP SDK FATAL ERROR

trying to get every post and comment from a facebook page, I made this function that should go through the pagination:
$req = $facebook->api("/" . $pagename . "/?fields=posts.fields(message,link,created_time,shares,likes,comments)");
function parcours_arbre($ab)
{
GLOBAL $facebook;
GLOBAL $pagename;
$next = create_request($ab['posts']['paging']['next']);
$next_req = $facebook->api($pagename.$next);
$ab_next = $next_req['data'];
$prev = create_request($ab['posts']['paging']['previous']);
$prev_req = $facebook->api($prev);
$ab_prev = $prev_req['data'];
if (empty($ab)) {
display_results($ab['posts']['data']);
} else {
parcours_arbre($ab_next);
parcours_arbre($ab_prev);
}
}
I unfortunately get the following error:
Notice: Undefined index: posts in /form.php on line 36
Notice: Undefined offset: 3 in /utils.php on line 20
Notice: Undefined offset: 4 in /utils.php on line 20
Fatal error: Uncaught GraphMethodException: Unsupported get request. thrown in /sdk/src/base_facebook.php on line 1271
Any idea how i could avoid it or what is going on? Would this go away if i use the "until" statement in my api request?
Thanks a lot,
To explain each error
the variable $ab which is an argument to the function, does not have a "posts" index. You should try to var_dump this variable so you can see what it actually looks like.
same as above
same as above
the api function takes 3. The path which should just be #pagename. The method ("GET" or "POST") most likely POST because GET is causing an error. The parameters, which should be array("fields" => "posts.fields(message,link,created_time,shares,likes,comments)")
I noticed that for next you have the code
$next_req = $facebook->api($pagename.$next);
but for previous you have
$prev_req = $facebook->api($prev);
Might want to look into this.

Error - Trying to get property of non-object

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? ;)

Categories