PHP - Grab the code that threw an error - php

I'd like to be able to grab the actual line of code (not the number, the line of code text) where a NOTICE error occurred. Is there any way to accomplish this? I can't seem to find a function that will return the code on a certain line # in the currently running script.

Assuming the PHP script has permissions to read the current file, you can do this:
<?php
echo $b; // Undefined variable
$errors = error_get_last();
$errorMessage = $errors['message'];
$pathToScript = $errors['file'];
$line = $errors['line'];
$arrayOfLines = file(__FILE__);
echo "The error message was: '$errorMessage occured in $pathToScript'";
echo "The line of code that caused the error is: \n";
highlight_string($arrayOfLines[$line-1]);
Output:
The error message was: 'Undefined variable: b occured in /path/to/script.php'
The line of code that caused the error is: echo $b;

Related

Notice: Undefined variable: dbRead in C:\vhosts\phpcs5\lesson07\workfiles\scripts\user_registration.php on line 28

I'm getting this error and after checking for similar errors I verified the syntax is correct. The complete error is;
WRITE OK
READ OK
Notice: Undefined variable: dbRead in C:\vhosts\phpcs5\lesson07\workfiles\scripts\user_registration.php on line 28
Fatal error: Call to a member function quoteInto() on a non-object in C:\vhosts\phpcs5\lesson07\workfiles\scripts\user_registration.php on line 28
My connection is defined as:
$dbwrite = new Zend_Db_Adapter_Pdo_Mysql($write);
$dbread = new Zend_Db_Adapter_Pdo_Mysql($read);
I did a test first to make sure it can see the database which is what the Write OK and Read OK are as follows;
if ($dbwrite->getConnection()) {
echo 'WRITE OK<br/>';
}
if ($dbread->getConnection()) {
echo 'READ OK';
}
So I'm not sure why I'm getting this error.
dbRead is undefined. You've defined $dbread. Note the capitalization.
Look through your code base (particularly user_registration.php line 28) and switch all instances to one or the other.

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
}

Can't use function return value in write context, PHP can't figure out why

This is my code :
PHP:
if(isset($_COOKIE("cookie_roof_angle")) && isset($_COOKIE("cookie_roof_direction")))
{
$roof_angle = intval($_COOKIE("cookie_roof_angle"));
$roof_direction = $_COOKIE("cookie_roof_direction");
$solarsell_page05_rendement = mysql_query("SELECT value FROM solarsell_page05_pvgis WHERE angle = $roof_angle AND azimut = " . $roof_direction. " ");
echo $solarsell_page05_rendement;
}
else
{
echo "no values";
}
I'm getting this error message :
Fatal error: Can't use function return value in write context in C:\xampp\htdocs\Development\phpFunctions.php on line 19
After some searching around on the web & stackoverflow.com, I found out it may be caused by the isset function, could anyone please explain if this is the problem and why?
If this is not the problem, maybe I did something wrong in my code part, but I can't figure out why.
The Cookies where both set when I got the error.
Sincerly,
Harmen Brinkman.
Syntax error:
$_COOKIE["cookie_roof_angle"] instead of $_COOKIE("cookie_roof_angle")

PHP: DOMNode::appendChild to an array of Elements

I am using DOMDocument to parse an XML file. I loop through the different Elements and see if any of them is missing and I fill an array with a createElement, with the error message. At the end I'm trying to appendChild that array but I always get the same error message:
Uncaught exception 'DOMException' with message 'Wrong Document Error'
DOMNode->appendChild(Object(DOMElement))
1 {main}
thrown in /xxx/xxx.php on line 235
PHP Fatal error: Call to undefined method DOMElement::item() in /xxx/xxx.php on line 235.
the code is as follow:
$SMQuery = new DOMDocument();
$SMQuery->loadXML($params);
$response = $SMQuery->createElement('SMreply');
$errors = array();
if (!$reqtyp = $SMQuery->getElementsByTagName("tag1"))
{$errors[] = $SMQuery->createElement('error', 'tag1 Element is missing');}
if (!$reqtyp = $SMQuery->getElementsByTagName("tag2"))
{$errors[] = $SMQuery->createElement('error', 'tag2 Element is missing');}
......
if(!empty($errors))
{
foreach($errors as $error) {
$response->appendChild($error); <==== this line is causing the error !!!
}
}
Any help is much appreciated.
Cheers,
Riki.
You don't show where $response is being defined, but if it's the result of another new DOMDocument(), then that explains you error - you can't add nodes from one DOM object to another directly. It has to be imported first via ->importNode(). Only after that can you actually append it.

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