Jankos
Jankos

Reputation: 11

Uncaught PDOException: SQLSTATE[HY000] [2002 How to fix this?

I need help with connection MySQL, I have a problem connecting to the database, what I am doing wrong ? Please send me an answer because I searched solution for about 2h and nothing.

This is a picture of the full error.

<?php
class Db {
   private $connection;

   public function __construct($servername, $dbname, $username, $password) {

       $this->connection = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
   }

   public function query($sql, $args = []) {
       if (!$args) {
           return $this->connection->query($sql);
       }

       $stmt = $this->connection->prepare($sql);
       $stmt->execute($args);
       return $stmt;
   }

   public function entryExists($table, $id) {
       $row = $this->query("SELECT * FROM $table WHERE id = $id")->fetch();

       if (!$row) {
           return false;;
       }
       else {
           return true;
       }
   }

   public function getRows($sql, $values = []) {
       $stmt = $this->query($sql, $values);
       return $stmt->fetchAll();
   }

   public function getFirstMatch($sql) {
       $stmt = $this->query($sql);
       return $stmt->fetch();
   }
}
?>

Upvotes: 0

Views: 353

Answers (1)

Juan Carlos Pacheco
Juan Carlos Pacheco

Reputation: 1

I think or are you sending empty values in your constructor function? Maybe you should try entering the values that it should take by default :) I hope my answer will help you. Excuse my English.

Upvotes: 0

Related Questions