How can I make error handling in this scenario and if there are errors, for example in connection or declaration, display them on the screen?
returnClient.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