I'm finding it difficult to call a variable from an auxiliary php code, which connects to my database, on my HTML page. I researched a lot of content, but I did not find a solution to this seemingly stupid problem.
My connection is being successfully completed, I have already tested. (connection.php):
<?php
$host="localhost";
$user="root";
$password="";
$dbname="database";
$con = new mysqli($host, $user, $password, $dbname);
if ($con->connect_error)
die("Connection failed: " . $con->connect_error);
?>
But when I include in the main file (include), when calling the $ con variable, the HTML page does not return correctly:
<?php
include('conexao.php');
$query = "select 'id', 'nome', 'sexo' from 'pessoas'"; ?>
<!DOCTYPE html>
<html>
<meta charset = "utf8">
<head>
<title> Empresa </title>
</head>
<body>
<header>
<h1> Empresa </h1>
</header>
<?php
if($stmt = $con -> prepare($query)) {
$stmt -> execute();
$stmt -> bind_result($id, $nome, $sexo);
?>
<table border = "1">
<tr>
<td>Id</td>
<td>Nome</td>
<td>Sexo</td>
</tr>
<?php
while($stmt -> fetch()) { ?>
<tr>
<td><?php printf("%s", $id) ?></td>
<td><?php printf("%s", $nome) ?></td>
<td><?php printf("%s", $sexo) ?></td>
</tr>
<?php
} ?>
</table>
<?php
$con -> close();
}
else
echo "Erro ao executar a consulta no banco de dados.";
?>
</body>
The error persists for all code. Where to call the variable $ con, it displays the code instead of executing a given command. Remember that I save the files inside a folder, and run from the host as per the image: