Jump to content
Fórum Script Brasil
  • 0

Verificar se conexão mysql ficou aberta com PDO


ndias

Question

Estou com duvida se minha conexão está encerrando.

 

Utilizo PDO para me conectar:

 

public static function getDb(){

        try {
          
            $db = new \PDO(
                'mysql:host=localhost;dbname=xyz;charset=utf8', 'xyz', 'xyz1234',
                array(
                    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
                )
            );

            return $db;

        } catch (PDOException $e) {
            echo "Erro de Conexão " . $e->getMessage() . "\n";
            exit;
        }

    }

public function __construct(\PDO $db)
    {
        $this->db = $db;

    }

public function verifica(){
        $sql = "SELECT * FROM tabela ";
            $result = $this->db->query($sql);
            $rows = $result->fetch();
            if($rows){

            }else{
                
            }
    }

 public function __destruct() {
        unset($this->db);
        foreach ($this as $key => $value) {
            unset($this->$key);
        }
    }

Estou fazendo certo? Como posso verificar se a conexão está ficando aberta?

Link to post
Share on other sites

2 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      148691
    • Total Posts
      644531
×
×
  • Create New...