Error printing echo php in div

Asked

Viewed 797 times

2

I’m an amateur programmer and I’m having a problem printing an eco php inside a div.

I have a file that when accessed (only once) will start the database installation:

mysqli_report(MYSQLI_REPORT_STRICT); 
try 
{
    $mysqli = new mysqli("localhost","root","");

    $mysqli->query("CREATE DATABASE IF NOT EXISTS respostas;");

    $ok = "Banco de dados criado com sucesso...";
} 
catch (Exception $e) 
{
    $error = "Desculpe alguma coisa não deu certo. Detalhes: " . $e->message;
}

And display the result in <div id="progress">

$pageBody = <<< EOPAGE
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>

<html lang="pt-br">
<head>
    <title>Home | Configuração </title>
    <meta http-equiv="Content-Type" content="txt/html; charset=utf-8" />
</head>

<body>  
    <div class="body">
        <div class="text">
            <p><h1>Bem Vindo</h1></p>
            <br/>
            <br/>
            <p class="texts"><i class="fa fa-cog"></i>Iniciaremos agora as configurações finais. Aguarde enquanto todos os arquivos são configurados.</p>
            <div id="progress">
            <p>Aguardando</p>
            <p>$ok</p>
            <p>$error</p>
            </div>
        </div>
    </div>
</body>
</html>
EOPAGE;

echo $pageBody;

However, when I call $ok the message is displayed normally, but when I call the $erro does not display error message, even simulated an error connection to the server, and the code gives error.

  • Put like this and test <?php echo $ok; ? > and <?php echo $error; ? > say something

  • @Césarsousa, unfortunately does not display the message. And when I put both, <?php echo $ok;?> and <?php echo $error>, returns an error message.

  • the correct would not be $e->getMessage ?

  • @Isvaldofernandes, tried using getMessage, however, did not work. Even leaving only the <p>$error</p> and simulating a connection failure, it does not return the message, but the code error.

1 answer

1


The Try/catch structure you used leaves one of the variables $ok and $error undefined (generates the error "Notice: Undefined variable").

I suggest using only one variable $message for both cases, and if your logic requires, a true/false variable indicating if there was an error:

$message = "mensagem ok";
$error = false;
try {
    // consultas
} catch (Exception $e) {
    $error = true;
    $message = "mensagem erro: " . $e->message;
}

In the comments you have been informed to use <?php echo $variavel; ?>, but the code in the question uses the heredoc syntax and variables are interpreted. To avoid problems it is good to surround variables inside strings with keys. Example: $variavel = "Texto com {$outraVariavel}";

Another detail that I noticed in your code, and that might be causing some error is on $pageBody = <<< EOPAGE, should be $pageBody = <<<EOPAGE, without the space before EOPAGE

  • Following the guidelines presented by @Sanction, and some small changes in the code, I solved the problem.

Browser other questions tagged

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