How to get error message from pg_insert()? - php

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?

Related

POST 500 (Internal Server Error) - Ajax & laravel 4

Sorry if my explanation is bad. Anyways, I have this todo list made by using laravel 4 sometime last year. Been working great for the last few months and I stopped playing around with it. Somehow I was playing around with it just yesterday and now it's giving me this POST 500 (internal Server ERROR). The Ajax call was working perfectly before. I had this error with chrome last night and today I tried using firefox, somehow it worked but then few minutes after, I am getting the same error in firefox too. By the way, a few ajax call is used here. Adding an item uses ajax to post what's added with a done button (this ajax is working perfectly). When the done button is clicked it should become a delete button and this is the ajax that's giving me the error. I went to the app.php to turn the debug => true
This error message is shown
{"error":{"type":"ErrorException","message":"Creating default object from empty value","file":"\/home2
\/.........\/TodoController.php","line":174}}
In my line 174 I have $query->done = 1;
that line is inside of this function
public function doubleDQuery($table, $id, $todoText, $done, $action){
$query = $table::where('id', '=', $id)->where('todoText', '=', $todoText)
->where('done', '=', $done)->first();
if($action == 'done'){
$query->done = 1;
$query->touch();
$query->save();
}
if($action == 'delete'){
$query->delete();
}
}
if I commented out the $query->done = 1, it gives me an error to the next line
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to a member
function touch() on a non-object","file":"\/home2\......\/TodoController
.php","line":175}}
if I commented the above line up, it gives me the error of none-object to save().
No modification is done for the past months and it was working for like a minute in firefox as mentioned above. I just have no idea where I should start looking for an answer, can someone give me a hand where I should start looking?
I have read that since 500 is internal server error it might be the server instead of my codes but it's weird that the other ajax call in the same application works just this is giving me the headaches.
That just means that $query is not what you think it is.
It seems to be null which means that there is no entry in the database matching your where clause. Although you expect it, respectively require it to be an object and also an instance of your model class for this code to work, without throwing an exception.
So you can either check for null manually and do whatever you wan't like:
if($query === null) {
//No matching row found
}
Or use firstOrFail() instead of first() which will actually already throw an exception with a 404 status code which may be what you want because you probably want to handle it properly on the client side.

Symfony 2.5 server returns 500 status

When I'm doing a request into API that I've written using Symfony 2.5. I'm sending form data using POST method and in the controller I'm doing
$params = json_decode($request->getContent(), true);
and if I do
var_dump($params);
I can see everything in console If I'm doing
console.log(data)
But If I try just do something like this:
array_keys($params)
Server returns me a 500 status code and error:
XMLHttpRequest cannot load http://host.loc/app_dev.php/posts. Invalid HTTP status code 500
You will always get 500 Error if something went completely wrong.
e.g. even use dump() in twig - which is not allowed in production
1) try the same page in /app_dev.php/[page]
- enable debug, this should give you alot of info about the error
2) look at the correct log file /app/logs/ as mentioned above
I guess you calling getContent() on a null Object ;-)

Show PHP errors as a string for display

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.

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.

SWF Upload: Returning errors

I've been messing around with SWF Upload, and I really like it. I was able to apply it very easily to my file upload class with very little issue. The problem I am having, however, is that I need to be able to send back errors to the uploader so a user knows if their file actually wasn't completely uploaded due to issues such as it being too large, or of the wrong format, ect. I read through the PHP example script that the writers of SWF Upload provide, but I cannot figure out how to pass back the errors to it. This is there error handling function:
/* Handles the error output. This function was written for SWFUpload for Flash Player 8 which
cannot return data to the server, so it just returns a 500 error. For Flash Player 9 you will
want to change this to return the server data you want to indicate an error and then use SWFUpload's
uploadSuccess to check the server_data for your error indicator. */
function HandleError($message) {
header("HTTP/1.1 500 Internal Server Error");
echo $message;
}
So the uploadSuccess function is expecting a response from the server. How would I go about encoding and sending that said response using "header()"?
If I use this:
header("HTTP/1.1 200 Uploaded File was Successful");
SWF Upload returns success, but that message does not come through to the uploadSuccess function.
I was also looking for a way to display an error message from the server. Based on the comments above, I was able to get this to work by updating the uploadSuccess() function in handlers.js to:
if (serverData != null && serverData != "ok"){
progress.setError();
progress.setStatus(serverData);
} else {
progress.setStatus("Complete.");
}
Meaning, if the response is anything other than "ok" it will be displayed as an error. This feels pretty hacky. I'd much rather be able to send a non-200 response from the server and have it show the error message from that, but as far I can tell that's not possible. Also, the way I've got it set up wouldn't work for custom messages that aren't errors.

Categories