error in finally with try cath

0

I am trying to create a% statica % for classe to mysql with conexão but is giving error on the PDO line

<?php

  namespace CONEXAO;

  use PDO; 

  class Conexao {

      public static $conexao;

      private $host     = "localhost";   
      private $db       = "funeraria2";
      private $user     = "root";
      private $password = "mysql";

      private function __construct() {

          try {

             self::$conexao = new PDO('mysql:
                                  host="'.$this->host.'";
                                  dbname="'.$this->db.'", 
                                  "'.$this->user.'", 
                                  "'.$this->password.'"
                               ');

          } catch (Exception $e) {

        self::$conexao = NULL;

        return self::$conexao;

        echo $e->getMessage();

        exit;

          } finally {

             return self::$conexao;

          }
      }


      public function fechaConexao () {

          if (self::$conexao != null) {

              self::$conexao = null;

          }
      }   
  }

I'm using finally as the editor of Dreamweaver and it gives me an error in the line as pictured below but I can not see any errors!

Change:

<?phpini_set("display_errors",true);
  ini_set("display_startup_erros",true);
  error_reporting(E_ALL | E_NOTICE | E_STRICT);

  namespace CONEXAO;

  use PDO; 

  class Conexao {

      private static final $conexao;

      private static final $host     = "localhost";  
      private static final $dbname   = "dbname";
      private static final $user     = "user";
      private static final $password = "password";

      public function __construct() {}

      public function abreConexao() {

          try {

             self::$conexao = new PDO('mysql:
                                              host=self::$host;
                                              dbname=self::$dbname', 
                                              self::$user, 
                                              self::$password
                                     );

          } catch (Exception $e) {

            self::$conexao = NULL;

            echo $e->getMessage();

          } 

      }


      public function fechaConexao () {

          if (self::$conexao != null) {

              self::$conexao = null;

          }

      }   

  }

  use CONEXAO\Conexao;
  $conexao =  new Conexao;
  $conexao->abreConexao();
    
asked by anonymous 18.06.2018 / 16:11

2 answers

0

Try changing the line where you make the connection, error 500 can have many causes, but I think that those line breaks and using the variables inside a string with ' instead of " is causing this problem.

self::$conexao = new PDO("mysql:host={self::$host};dbname={self::$dbname}", self::$user, self::$password);
    
18.06.2018 / 17:49
0

Solution:

Class:

<?php

  namespace CONEXAO;

  ini_set("display_errors",true);
  ini_set("display_startup_erros",true);
  error_reporting(E_ALL | E_NOTICE | E_STRICT);     

  use PDO; 

  class Conexao {

      private static $conexao;
      private static $host     = "localhost";    
      private static $dbname   = "dbname";
      private static $user     = "user";
      private static $password = "password";

      public function __construct() {

          try {

             self::$conexao = new PDO('mysql:
                                              host='.self::$host.';
                                              dbname='.self::$dbname, 
                                              self::$user, 
                                              self::$password
                                     );

          } catch (Exception $e) {

            self::$conexao = NULL;

            echo $e->getMessage();

          } 

      }

      public function abreConexao() {

          return self::$conexao;

      }


      public function fechaConexao () {

          if (self::$conexao != null) {

              self::$conexao = null;

          }

      }   

  }

File that calls the class:

  use CONEXAO\Conexao;
  $conection =  new Conexao;
  $conexao = $conection->abreConexao();

  $str = "SELECT * FROM administradores";
  $query = $conexao->prepare($str);     
  $query->execute();
  $resultado = $query->fetchAll( PDO::FETCH_ASSOC );
  print "<pre>";
  print_r( $resultado  );   
  print "</pre>";
    
18.06.2018 / 18:16