Salute the people, krch I do not want to connect ...

mysql.php

<? class Mysql { private $db; private $config; public function __construct(){ $this->config = include __DIR__ .'/config.php'; $this->db = new PDO("mysql:dbname={$this->config['dbname']};host={$this->config['dbhost']}, {$this->config['dbuser']}, {$this->config['dbpassword']}"); } function query($sql, $params=[]) { $sth - $this->db->prepare($sql); $sth = $sth->execute($params); return $sth->fetchAll(); } } 

This is the error I get when I create a class:

Warning: PDO :: __ construct (): php_network_getaddresses: getaddrinfo failed: . in E: \ OpenServer \ OpenServer \ domains \ blackmarket \ core \ mysql.php on line 9

Fatal error: in E: \ OpenServer \ OpenServer \ domains \ blackmarket \ core \ mysql.php on line 9

  • one
    Give the value of dbhost - ReinRaus
  • Try to correct the problem with the encoding in the logs to see the hidden part of the error message. - VenZell
  • If my answer helped you, vote for it and mark it right by clicking on the check mark to the left of the question - VenZell

1 answer 1

First of all, it is worth checking whether the host specified in the settings is available for connection. Check its availability using the ping and nslookup commands. If the hostname is a domain name, try specifying an IP address instead.


Based on the answer to the question:
PHP error: php_network_getaddresses: getaddrinfo failed: (while getting information from other site.)


UPDATED

PDO :: __ construct

You have an error in this line (in the $dsn error parameter):

 $this->db = new PDO("mysql:dbname={$this->config['dbname']};host={$this->config['dbhost']}, {$this->config['dbuser']}, {$this->config['dbpassword']}"); 

Right like this:

 $this->db = new PDO("mysql:dbname={$this->config['dbname']};host={$this->config['dbhost']}", $this->config['dbuser'], $this->config['dbpassword']); 
  • I should have also attached a config: <? return array ('dbhost' => 'localhost', 'dbname' => 'blackshop', 'dbuser' => 'root', 'dbpassword' => ''); - cr1gger
  • I don't think it's worth pinging to check localhost. This is a local host well ... - cr1gger
  • @ cr1gger, I just saw your config just now, the assumption was for the general case. - VenZell
  • @ cr1gger, I added my answer - VenZell
  • Thanks, I understood my mistake. - cr1gger