Error printing echo php into div

2

I am an amateur programmer and I have a problem printing a php echo inside a div.

I have a file that, when accessed (one time only) will start the installation of the database:

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 $erro it does not display error message, even simulated a connection error with the server, and the code gives error.

    
asked by anonymous 01.03.2015 / 15:56

1 answer

1

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

I suggest that you use only a variable $message for both cases, and if your logic needs, 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 comments you were told to use <?php echo $variavel; ?> , but the code in question uses syntax heredoc and variables are interpreted. To avoid problems it is good to surround variables inside strings with braces. Example: $variavel = "Texto com {$outraVariavel}";

Another detail that I noticed in your code, which may be causing an error is $pageBody = <<< EOPAGE , should be $pageBody = <<<EOPAGE , without the space before EOPAGE

    
02.03.2015 / 12:24