1
I have a BD query function that needs the function conectar which is in another file, in her file I did not give include, I preferred to put the include before calling the function consultar, because there may be cases where the path changes.
I’m having a problem displaying a custom message when it doesn’t find the function:
@$conexao = conectar();
if($conexao == false or $conexao == NULL)
{ //falta include de conexao.php
echo "Não há uma conexão ativa com o seu banco de dados!\n<br><i>Inclua a página ../conexao.php<br>";
return false;
}
Connect function:
function conectar()
{
$dbhost = "localhost";
$dbname = "teste";
$dbuser = "root";
$dbpass = "";
try
{
$conn = new PDO("mysql:host={$dbhost};dbname={$dbname}", $dbuser, $dbpass);
$conn ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return true;
}
catch (PDOException $ex)
{
echo "Erro de conexão: " . $ex->getMessage();
return false;
}
}
As I said, the functions conectar and consultar are in separate files and did not put the include 'conexao.php' in the file of the other function to not give path error, doubt is how to display the echo when my function conectar is not defined, that is, when it is not found.
What happens? or what doesn’t happen? You really need that arroba?
– rray
He doesn’t get into the
ifwhen I take off the link function include– Leonardo
Are you using the PDO? Do you happen to have a
try-catchwithin the function?– rray
Yes, yes, I completed the post with the function and more description of the problem.
– Leonardo