Me sale el siguiente error:
Fatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in C:\xampp\htdocs\StudioGamer\index.php:8 Stack trace: #0 C:\xampp\htdocs\StudioGamer\index.php(8): PDO->prepare('SELECT id, nomb...') #1 {main} thrown in C:\xampp\htdocs\StudioGamer\index.php on line 8
Ya revise todo mi código y no le encuentro solución, espero puedan ayudarme.
<!--database.php-->
<?php
class Database{
private $hostname="localhost";
private $database ="gamer_studio";
private $username = "root";
private $password = "1234";
private $charset = "utf8";
function conectar()
{
try{
$conexion = "mysql:host=" .$this->hostname . "; dbname" . $this->database . "; charset" . $this->charset;
$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 conexion: ' . $e->getMessage();
exit;
}
}
}
?>
<!--index.php-->
<?php
require 'config/database.php';
$db = new Database();
$con = $db->conectar();
$sql = $con->prepare("SELECT id, nombre, precio FROM productos WHERE activo=1");
$sql->execute();
$resultado = $sql->fetchAll(PDO::FETCH_ASSOC);
?>