Adebayo Ojo
asked on
How to catch sql error with Ajax.
My PHP script returns an SQL error:
NOTE: The PHP script is - feedUpdate.php and it contains the expected result. The above ajax returns the required result from the script if I have db connection. But once the db connection is lost, it pops up the sql error on the web page. Please how can I pick that sql error and present something like:
SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.whenever it cannot connect to the database to fetch the expected result. This SQL error always comes up on my webpage and I would like to present it with something nice to my page visitors. But my Ajax seems not to be working as it always comes up with that error each time I lost db connection. Below is my ajax:
function feedUpdate() {
//$(document).ready(function() {
$.ajax({
type: 'POST',
url: 'functions/feedUpdate_New.php',
success: function(data) {
if (data) {
document.getElementById("homeFeed").innerHTML = data;
} else {
document.getElementById("homeFeed").innerHTML = "We are not able to get the feed at this time";
}
}
});
//});
}
feedUpdate();
NOTE: The PHP script is - feedUpdate.php and it contains the expected result. The above ajax returns the required result from the script if I have db connection. But once the db connection is lost, it pops up the sql error on the web page. Please how can I pick that sql error and present something like:
We are not able to get the feed at this time
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As Dave said, that is your valid return and it is your error. The issue is your php code on feedUpdate_New.php couldn't connect to the database.
ASKER
@Dave Baldwin,
Yes, AJAX saw everything from the PHP as valid return, but can't I separate the return result from PHP like below:?
NOTE: When I did this, it didn't still work, as the string "We are not able to get the feed at this time" were been output by AJAX whether I have DB connectivity or not. So my question again is, how can I separate the PHP result on AJAX and output the expected result on my webpage?
Yes, AJAX saw everything from the PHP as valid return, but can't I separate the return result from PHP like below:?
function feedUpdate() {
//$(document).ready(function() {
$.ajax({
type: 'POST',
url: 'functions/feedUpdate_New.php',
success: function(data) {
if (data == "SQLSTATE[HY000] [2002]") {
document.getElementById("homeFeed").innerHTML = "We are not able to get the feed at this time";
} else {
document.getElementById("homeFeed").innerHTML = data;
}
}
});
//});
}
feedUpdate();
NOTE: When I did this, it didn't still work, as the string "We are not able to get the feed at this time" were been output by AJAX whether I have DB connectivity or not. So my question again is, how can I separate the PHP result on AJAX and output the expected result on my webpage?
'data' will never equal just "SQLSTATE[HY000] [2002]", it will be the whole string you posted above.
Yeah, this should catch it, although I would catch it in the PHP and return a 500 exception making the AjaxError be thrown:
if (data.indexOf("SQLSTATE[HY000]") > -1) {
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Selected best answers
$(document).ajaxError(func
alert("An error occurred!");
});
OR
if(mysql_query($query)){
$response_array['status'] = 'success';
}else {
$response_array['status'] = 'error';
}
also check below URL for reference.
https://www.sencha.com/forum/showthread.php?238414-Ajax-Request-Error-Handling