Display message instead of error?

1

I have a query function in DB that needs the conectar function that is in another file, in the file I did not give include , I preferred to put include before calling the consultar function, since there may be cases the path changes.

I'm having a problem while displaying a custom message when it does not 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 I did not put the include 'conexao.php' in the file of the other function to not give path error, the question is how to display echo when my function conectar is not set, that is, when it is not found.

    
asked by anonymous 23.12.2015 / 20:14

1 answer

2

To find out if a function exists or has already been set use function_exists ()

if(function_exists('conectar')){
  $conexao = conectar();
}else{
  echo 'função não foi definida';
}
    
23.12.2015 / 20:30