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
– Maniero
@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.
– user60252
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.
– Maniero