Error handling in consultation

Asked

Viewed 54 times

4

How can I handle errors in this scenario and if there are errors, for example in connection or in the statement, display them on the screen?

returnCliente.php

<?php 

        $hostname="localhost";  
        $username="USUARIO";  
        $password="SENHA";  
        $db = "Nome_DB";  
        $pdo = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);

    $assunto = filter_input(INPUT_GET, 'term', FILTER_SANITIZE_STRING);


    $buscar = $pdo->prepare('SELECT NOME_LOJA FROM lojas WHERE NOME_LOJA LIKE ? ORDER BY NOME_LOJA ASC');
    $buscar->execute(array("%$assunto%"));

    while ($results = $buscar->fetch())
    {
        $data[] = $results['NOME_LOJA'];
    }


    //SQL para selecionar os registros
    $result_msg_cont = $pdo->prepare('SELECT assunto FROM mensagens_contatos WHERE assunto LIKE ? ORDER BY assunto ASC LIMIT 7');
    $result_msg_cont->execute(array("%$assunto%"));

    while ($row_msg_cont = $result_msg_cont->fetch())
    {
        $data[] = $row_msg_cont['assunto'];
    }


    echo json_encode($data);

?>

HTML

  ............
  .............
  <input type="text" id="assunto" name="assunto">
  </form>

    <script type="text/javascript">
        $(document).ready(function(){
            $("#assunto").autocomplete({
                source: 'retornaCliente.php' 
            });
        });

    </script>
    ...............
    ...............

The treatment I used

    try{
        $pdo = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
    }catch(PDOException $e){
        exit("Erro na conexão com a base de dados");

    }

but the difficulty is to show some message on the screen

  • Please avoid long discussions in the comments; your talk was moved to the chat

  • @Manieiro, I understand and I will follow this guideline, just explain to me what are the drawbacks/problems of these long discussions in the comments.

  • The system always signals when it occurs. You are running away from the goal of the site that is to have questions and answers, when you have multiple comments is because either the question or the answer is not good and needs too much discussion. Comment here or there to help at some dark point is normal, but when it gets a lot is because there is something wrong and the system sends a moderator to intervene not to become forum.

1 answer

3

The answer to this question is in fiddle @Sam indicated in his comment and @Bacco’s Soen nomination. But as these links may be non-existent in the future, I will put the solution here.

javascript

$(document).ready(function(){
    $( "#assunto" ).autocomplete({
        minLength: 2,
        source: function( request, response ) {
            $.ajax({
                url: "retornaCliente.php",
                data: { query: request.term},
                success: function(data){
                    response(data);
                },
                error: function(jqXHR, textStatus, errorThrown){
                  //  coloque aqui a mensagem que quiser
                  //$("...").modal('show'); 
                  //alert (...); 
                  //exibir numa div                   
                },
              dataType: 'json'
            });
        }
    });   
});

PHP

$assunto = filter_input(INPUT_GET, 'query', FILTER_SANITIZE_STRING);
  • But that’s the thing. The function of source is very restricted and does not allow handling errors.

  • @Sam, hahaha, the people of Brasilia are kind of discredited! But it’s easy to believe my version. :)

  • @Sam go to sleep, tomorrow walk on the road, come back Monday, hug!

  • Big hug brother. Bon voyage!

Browser other questions tagged

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