How to display the error coming from Mysql?

Asked

Viewed 70 times

3

I made the code below, and works perfectly, but in case Mysql returns some error, as I will know?

I have no idea, someone can help me?

The message should return on alert, but I managed to create the alert of Sucesso, but how do 'Alert' with error return when 'false' ???

Follows the code:

var nome_arquivo_api = "clientes.php";    
var end_arquivo_api = "api/include/";
var complemento_api = "?action=";  
var lista_excluir_api = "lista_excluir";

var url_excluir_api = end_arquivo_api+nome_arquivo_api+complemento_api+lista_excluir_api;   

$scope.apagarRegistro = function (id) {
    for (i in $scope.listagem) {
        if ($scope.listagem[i].id == id) { 
            $http.post(url_excluir_api,
                    {
                        'id': id
                    }
            )
            .success(function (data, status, headers, config) {
                $http.get(url_listar_api).success(function (data) {
                    $scope.listagem = data;
                    $.gritter.add({
                        title: '<div style="color:#43A608;">ATENÇÃO</div>',
                        text: 'Registro Excluído com Sucesso!',
                        image: 'assets/images/icon_sucesso.png',
                        sticky: false,
                        time: ''
                    });
                });
            });
        }
    }
}

PHP file:

function lista_excluir() {

$data = json_decode(file_get_contents("php://input"));
$item_id = $data->id;

if (PDO_excluirRegistro("administradores", $item_id, "id")) {
    return true;
} else {
    return false;
}
}

PHP function:

function PDO_excluirRegistro($tabela, $id, $campo_chave) {
$pdo = conectarBanco();
try {
    $deletar = $pdo->prepare("DELETE FROM $tabela WHERE $campo_chave = ?");
    $deletar->bindValue(1, $id);
    $deletar->execute();

    if ($deletar->rowCount() == 1) :
        return true;
    else :
        return false;
    endif;
} catch (PDOException $error) {
    echo "<h4>";
    echo "Mensagem de Erro: " . $error->getMessage();
    echo "</h4>";
}}
  • Have you tried using the browser console? And include the console tag in your js? console.log(var);

1 answer

3


Your PHP code needs to return an HTTP code other than 200. Simply returning true or false is not sufficient - in both cases, the $http.post() returns successively, but simply the success has the value of false.

In PHP, you would have to change the Headers of the returning message. A very simple code would be using the function header():

function lista_excluir() {

$data = json_decode(file_get_contents("php://input"));
$item_id = $data->id;

if (PDO_excluirRegistro("administradores", $item_id, "id")) {
    header("HTTP/1.1 200 OK");
    return true; // Aqui, você devolve o valor 'true', porém poderia conter HTML ou JSON também
} else {
    header("HTTP/1.1 500 Internal Server Error");
    return false; // Mesmo aqui
}
}

More information

Web technologies (such as javascript, and therefore Angularjs) use the HTTP/1.1 standard. This standard defines several return codes. They are called HTTP Status Codes, or HTTP status codes. There are many resources to learn more about them, one of them this being the Wikipedia article.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.