Finally error with Try Cath

Asked

Viewed 53 times

0

I’m trying to create a classe Statica for conexão at the mysql with PDO but is making a mistake in the line of finally

<?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 the Dreamweaver as editor of códigos and it gives me an error in line as picture below but I can not see any error! inserir a descrição da imagem aqui

Altering:

<?php

  ini_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();
  • What is the error? puts the message in the question.

  • 3
    1. If you run the code, is there an error on this line? If not, your editor is poorly configured or does not support such syntax; 2) It makes no sense to run a return in the catch having the Finally; 3) It makes less sense even if you give return inside the builder.
  • So I made a change to the code and put it at the end of the question. I created an open functionConexao() just for that. Yes: still giving server error (500). But in the console gives Request URL: data:image/png;Base64,iVBORw0KGgoAAAANSUhEUgABNEAABECAAAACKI/xBAAAAAAnRSTlMAAAHaTzTgAAn9SURBVHgB7J1Rktu8EYRbKSV/cBEeZfcCOOh8fFAeyj8CLzqNh2dRnroTmFABYEar7UL8GDGUhc7rYagMjA... huge

  • 1

    What is your version of PHP? The finally was only included in version 5.5. Another thing, this request you cite is from an image, does not seem to be related to the problem.

  • PHP Version 7.2.1

  • removed Finally and still giving ero 500. Code at the end of the question

  • You will need to check the server logs to see the error. Always gave 500? Or started with recent changes?

  • Need to look at apache error log.

  • I sent one last correction at the end of the question. I think the problem is in the concatenation of variables in the call to PDO. each parameter is bounded pro '', it is 3 in all. but when I want to put a variable in place of the parameter gives error. Please. Take a look at me like I did at the end of the question

  • can see the script running at http://www.funerariasaopedro.net.br/crud/Conexao

Show 5 more comments

2 answers

0

Try changing the line where you make the connection, error 500 can have many causes, but I think those line breaks and use 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);

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 calling 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>";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.