No linux, ubunto, estou com o seguinte erro ao utilizar a classe de conexão pdo. HP Fatal error: Uncaught Error: Call to a member function bindValue() on bool in /home/carlos/Projects/alura/php/pdo/inserir-aluno.php:18 Stack trace:
#0 {main} thrown in /home/carlos/Projects/alura/php/pdo/inserir-aluno.php on line 18
O mesmo acontece com o Select: PHP Fatal error: Uncaught Error: Call to a member function fetchAll() on bool in /home/carlos/Projects/alura/php/pdo/lista-alunos.php:10 Stack trace:
#0 {main}
Seu passo a conexão diretamente funciona:
$caminhoBanco = __DIR__ . '/banco.sqlite';
$pdo = new PDO('sqlite:' . $caminhoBanco);
class ConnectionCreator
{
public static function createConnection(): PDO
{
$databasePath = __DIR__ . '\..\..\..\banco.sqlite';
return new PDO('sqlite:' . $databasePath);
}
}
<?php
use Alura\Pdo\Domain\Model\Student;
require_once 'vendor/autoload.php';
$pdo = \Alura\Pdo\Infrastructure\Persistence\ConnectionCreator::createConnection();
$student = new Student(
null,
"Patricia Freitas",
new \DateTimeImmutable('1986-10-25')
);
$name = $student->name();
$sqlInsert = "INSERT INTO students (name, birth_date) VALUES (:name, :birth_date);";
$statement = $pdo->prepare($sqlInsert);
$statement->bindValue(':name', $student->name());
$statement->bindValue(':birth_date', $student->birthDate()->format('Y-m-d'));
if ($statement->execute()) {
echo "Aluno incluído";
}