Hello, I am new to this, I am trying to make a query prepared to display data from a table, I have the following files
Blog.php:
<?php
require 'config/Connection.php';
require 'config/ClassConnection.php';
require 'config/CreateCard.php';
?>
Connection.php
private $hostname = "localhost";
private $database = "brand-right";
private $username = "root";
private $password = "dev123456";
private $chatrset = "utf8";
function conectar(){
try{
$conexion = "mysql:host=" . $this->hostname . "; dbname" . $this->database . ";charset=" . $this->chatrset;
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false
];
$pdo = new PDO($conexion, $this->username, $this->password, $options);
return $pdo;
}catch(PDOException $e){
echo 'Error de conexion:' . $e->getMessage();
exit;
}
}
}
ClassConnection.php
$db = new Database();
$con = $db ->conectar();
CreateCard.php
$sql = $con->prepare("SELECT id, title, extract, body, outing FROM tb_articles");
$sql->execute();
$resultado = $sql->fetchAll(PDO::FETCH_ASSOC);
The error it throws me is the following:
Fatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in C:\Apache24\htdocs\Projects\BrandRight\config\CreateCard.php:3 Stack trace: #0 C:\Apache24\htdocs\Projects\BrandRight\config\CreateCard.php(3): PDO->prepare('SELECT id, titl...') #1 C:\Apache24\htdocs\Projects\BrandRight\blog.php(4): require('C:\Apache24\htd...') #2 {main} thrown in C:\Apache24\htdocs\Projects\BrandRight\config\CreateCard.php on line 3
The error occurs when I import the CreateCard.php file, that is, before that, the connection works perfectly