problem connecting via pdo

1

I'm using pdo to connect to a mysql database, before it worked perfect, but now something has broken. The error messages that appear are:

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\taller-poo-a\app\database\DatabaseConnector.php on line 16

and the other error is:

Connection failed: SQLSTATE[HY000] [2002] Solo se permite un uso de cada direcci�n de socket (protocolo/direcci�n de red/puerto) 
Sometimes there is an error, sometimes the other comes out, I do not understand anything anymore.

The class of the connection is this:

 class DatabaseConnector
{
    private $db_host = 'localhost';
    private $db_name = 'taller-php';
    private $db_user = 'root';
    private $db_pass = '';

    private $connection;

    function __construct()
    {
        try{
            $this->connection = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", 
                                    $this->db_user, 
                                    $this->db_pass); 
            $this->connection->exec("set names utf8");
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch (PDOException $e) {

            echo 'Connection failed: ' . $e->getMessage();

            }
    }

    public function getConnection(){

        if ($this->connection === null) {
            $this->connection = new self();
        }
        return $this->connection;

    }
}

and the user class where I am using it has this method:

public function getAllUsers(DatabaseConnector $connector){
    try{

        $connection = $connector->getConnection();
        $preparedQuery = $connection->prepare('select * from users');
        $preparedQuery->execute();

        $users = $preparedQuery->fetchAll(PDO::FETCH_ASSOC);

        return $users;
    }catch(PDOException $e){
        echo "Error : " . $e->getMessage();
    }

}

I'm making the connection as always, but I do not know what happens.

    
asked by kmilo93sd 03.04.2018 в 22:48
source

0 answers