Show PHP errors as a string for display - php

I'm trying to find out how I would put a PHP error into a variable to display.
Similar to the mysql_error(); I'd like to be able to have displayed the error that PHP gives?
I'm using json_encode to send the testing user back a value from the requested PHP page but if an error happens I can get the mysql_error(); fine but how would I get the php error?
I've tryed $phpError = error_reporting(); but I just get error 22527 like this same Error. I just want to put the php error into a string to send back to the user.

You can write your own error handler: http://www.php.net/manual/en/function.set-error-handler.php
And return a json encoded string.

you can create an custom error handler with set_error_handler and from that point on you can do anything you want

Check out $php_errormsg. I hope it helps.

Related

How to get error message from pg_insert()?

I'm using a PHP function pg_insert() to insert data to PostgreSQL. When the function return false, I can not get error message by pg_last_error().(It works for pg_query).
Is there a way that I can get an error message from pg_insert()?
I believe it arrives too late. However I'll post something I just found dealing with the same problem.
When I use
$res = pg_insert($dbconn, 'mytable', $assocArray, PGSQL_DML_ESCAPE);
I don't get any error. However, if no options are passed,
pg_last_error($dbconn)
Displays the error properly. Could that be your case?

PHP and AJAX - internal error 500

I am trying to make a simple AJAX calling in PHP and have a problem with Internal error 500. When I click on the link, so the data are successfully loaded - this I see through FireBug, but I got the error above. It's on localhost.
When I try to set to the URL of a browser the address, that is called by AJAX, so the content is successfully loaded.
Where I should start to search the problem - or what could be wrong?
EDIT: in apache log is nothing weird, looks fine.
If after checking your php error log you don't find any issues, could it be your javascript AJAX call expects results to be returned in a specific format like JSON?
If this is the case, you need PHP to set the correct content type header when it is responding to the AJAX call, like so:
header('Content-type: application/json');
So in context, this might look something like:
$some_data = array(
'user_id' => 47,
'first_name' => 'Mike',
);
header('Content-type: application/json');
echo json_encode($some_data);
Look in the PHP error logs, or perhaps even your web server's error log.
Or if your script spews out errors to the client (which it hopefully doesn't do, at least not in production), try taking a look at the response in Firebug.
You most likely have a bug in your PHP. It doesn't matter if it is an AJAX call. Look at your server logs.

Tried to hide any error messages, but it still appears

I'm using php 5.2.13 and Oracle database.
And I'm trying to hide error messages, but doesn't work.
I tried to...
add this code to the page where an error occurs
ini_set(display_errors,0);
set an option in php.ini
display_errors = Off
...and what else should I do?? I thought I had done enough.
And the error message is coming from Oracle like this;
ORA-01400 cannot insert NULL into .....
I wonder if there's any modules in php that display error messages..?
thanks.
This is not a PHP error, but a Oracle error, your column can't be null - you need to specify a value. That's why your display_error's doesn't hide this error.
I have to emphasize - like others in the comments - that the only good error is a fixed one.
How about using LOG ERRORS INTO on your DML, this should allow you to dump your errors to a table for later inspection.
see: http://www.oracle-developer.net/display.php?id=329
and: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_9014.htm#BGBDIGAH

change php error output to custom message with link

How do I change the error output of a php error? For example if its a syntax error, or a server side time out, i want to echo a message that allows the user to refresh the page.
Heres the code I want to add my error message to:
$XML->registerXPathNamespace('tree','www.tree.com'); <--occasionally errors here, so I want to output my own error message.
Use the function set_error_handler to define a custom function to be called when there is an error. You can then decide to do whatever you want within that function with the error.
If you only want it for a specific duration, you can restore it afterwards with restore_error_handler.
set_error_handler('yourHandler');
...
$XML->registerXPathNamespace('tree','www.tree.com');
...
restore_error_handler();
function yourHandler(int $errno , string $errstr) {
//show link to refresh page, whatever. full signature can be found on PHP manual page
}
Well, quick check in php manual for registerXPathNamespace shows that it returns TURE or FALSE. Which is perfect for my solution.
if (!#$XML->registerXPathNamespace('tree','www.tree.com')) {
echo '<b>ERROR:</b> Could not register xml path. Please reload the page!';
}
Not that, if it returns FALSE, then the message will be displayed. And the # in front of $XML will disable the original errors that, that action may cause.

Unknown problem with PHP's XML parsing functions

I'm using a PHP4 implementation of SimpleXML, which uses the built-in xml_* functions from PHP 4. I have an odd problem that I'm unable to diagnose due to no error reporting on the server and not being able to turn on error_reporting.
I've modified the Parse() function to include this:
[stuff here to initialise the parser]
echo '<textarea rows="8" cols="50">', htmlspecialchars($this->xml), '</textarea>';
$parsed = xml_parse($this->parser, $this->xml) or die('error with xml_parse function');
The textarea displays the XML fine, and the XML itself is perfectly valid. But the page stops right after that and doesn't appear to call the xml_parse function, or output the 'die' message.
Should also add that this works fine on other pages, it just appears to be a problem with this particular page for some reason.
What could be happening here? Are there other ways to debug this?
Yes, you can debug this by adding this to your script:
error_reporting(E_ALL);
Thus will turn on error reporting.
Also, using ‘or die‘ only works if the function call before it returns a falsey value. In the event of fatal errors, it doesn't help.
Try to set the error reporting on and see what error comes though, it is likely there is some fatal error coming up:
ini_set('display_errors', true);
error_reporting(E_ALL);
Try getting the XML specific parser error message:
echo xml_error_string(xml_error_code($parser));
IIRC, these are not output by default no matter what your error reporting is set to.
Reference:
xml_get_error_code
xml_get_error_string
It seems that the problem was due to the XML being too big for the parser. Since we can't turn on error_reporting there was no way to get proper debugging info.
I decided to use a separate script the generate the HTML for the page, to avoid issues with the page going down occasionally.

Categories