Display message instead of error?

Asked

Viewed 329 times

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?

  • He doesn’t get into the if when I take off the link function include

  • Are you using the PDO? Do you happen to have a try-catch within the function?

  • Yes, yes, I completed the post with the function and more description of the problem.

1 answer

2


To know if any function exists or has already been defined use function_exists()

if(function_exists('conectar')){
  $conexao = conectar();
}else{
  echo 'função não foi definida';
}
  • I just found in php rsrs documentation Thanks for your attention!

Browser other questions tagged

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