Error SQL 1046 Nie wybrano bazy danych

PHP  Założony przez  Patryk Stefański.

Ogólnie nastukałem szybko jakąś klasę odpowiadającą za baze danych i podczas testów wyskoczył mi błąd

Fatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in E:\xampp\htdocs\zqscript\pages\class\database.class.php:82

Linie 82 zaznaczyłem public function execute()  // LINIA 82
Klasa Db
<?php
/*
    @Database Class
    @Version 1.0
    @Author ZnaQu

*/
class Database {

    /**
     * @var db_host
    */
    private $db_host;

    /**
     * @var db_name
    */
    private $db_name;
    
    
/**
     * @var db_user
    */
    private $db_user;
    
    
/**
     * @var db_password
    */
    private $db_password;
    
    
/**
     * @var dbh
    */
    private $dbh;
    

    
public function __construct($db_host$db_name$db_password$db_user)
    {
        try {
            $dns "mysql:host=" $this->db_host ";dbname=" $this->db_name;
            $options = [  
                PDO
::ATTR_PERSISTENT => true 
                PDO
::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION  
            
]; 
            $this->dbh = new PDO($dns$this->db_user$this->db_password$options);
        }
        catch(PDOException $e) {
            print "Error!: " $e->getMessage() . "<br/>";
            die();
        }
    }

    /**
     * @var stmt
    */
    private $stmt;

    public function query($query
    {
        $this->stmt $this->dbh->prepare($query);
    }
    public function bind($param$value$type null)
    {
        if (is_null($type)) {  
            
switch (true) {  
                
case is_int($value):  
                $type 
PDO::PARAM_INT 
                
break;  
                
case is_bool($value):  
                $type 
PDO::PARAM_BOOL 
                
break;  
                
case is_null($value):  
                $type 
PDO::PARAM_NULL 
                
break;  
                
default:  
                $type 
PDO::PARAM_STR 
            
 
        
}
        $this->stmt->bindValue($param$value$type);  
    
}
    public function execute()  // LINIA 82
     
        
return $this->stmt->execute();  
    
}
    public function resultset()
     
        $this
->execute();  
        
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);  
    
}
    public function single()
     
        $this
->execute();  
        
return $this->stmt->fetch(PDO::FETCH_ASSOC);  
    


Db.php
<?php

require_once CLASSS."database.class.php";
$db = new Database("localhost""zqscript","123456","root"); 
Home.php
<?php
require_once PAGES"database.php";

$query "SELECT * FROM _news";
$stmt $db->query($query);
$stmt $db->execute();


$smarty->assign("result"$stmt);
$smarty->display("home.tpl"); 
W __construct() nie używaj $this->variable do podnoszenia obiektu, bo one są puste (mają wartość null):
$this->dbh = new PDO($dns$this->db_user$this->db_password$options); 

Swoją drogą nie wiem po co tworzysz własną klasę do tego, skoro - w domyśle - ma działać tak jak PDO
Czyli taka klasa, którą napisałem jest nie potrzebna ponieważ PDO jest taką klasą już samo w sobie ?
Tak np. wygląda moja klasa, którą zaimplementowałem we własnym CMSie i która rozszerza PDO.
<?php

declare(strict_types=1);

namespace App\Service;

use PDO;

final class MyPDO extends PDO
{
   public function __construct()
   {
       $driver = getenv('DB_DRIVER') ?: 'mysql';
       $host = getenv('DB_HOST') ?: 'localhost';
       $dsn = sprintf('%s:host=%s;dbname=%s;charset=utf8', $driver, $host, getenv('DB_NAME'));

       try {
           parent::__construct($dsn, getenv('DB_USER'), getenv('DB_PASS'));
       } catch (PDOException $e) {
           die($e->getMessage());
       }

       $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
       $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
   }
}

I używam jej w ten sposób:
<?php

declare(strict_types=1);

namespace App\Repository;

use App\Service\MyPDO;

abstract class AbstractRepository implements RepositoryInterface
{
   protected $pdo;
   protected $prefix;
   protected $tableName;

   public function __construct(MyPDO $pdo, string $tableName)
   {
       $this->pdo = $pdo;
       $this->prefix = getenv('DB_PREFIX') ?: '';
       $this->tableName = $tableName;
   }

   protected function getFullTableName(): string
   {
       return $this->prefix . $this->tableName;
   }

   public function __destruct()
   {
       $this->pdo = null;
       $this->prefix = null;
       $this->tableName = null;
   }
}

Ty natomiast możesz spokojnie podnieść klasę przez:
<?php
$pdo = new MyPDO();
I też będzie działać.



Użytkownicy przeglądający ten wątek:

1 gości