Answer the question
In order to leave comments, you need to log in
What's wrong here?
Good day comrades. I have a snag, there is a class Db , the connection is registered in it by the constructor, it is carried out, it does not give errors when wardumping, I make such a request from the main page
require __DIR__.'/autoload.php';
$dbh = new \App\Db();
$sql = 'SELECT * FROM about';
$article = $dbh->query($sql,[]);
var_dump($article);die;
namespace App;
class Db
{
protected $dbh;
public function __construct()
{
$config = (include __DIR__.'/../../config.php')['db'];
$this->dbh = new \PDO('mysql:host'.$config['host'].';dbname='.$config['dbname'], $config['login'], $config['password']);
}
public function execute(string $sql)
{
$sth = $this->dbh->prepare($sql);
return $sth->execute();
}
public function query(string $sql, array $data)
{
$sth = $this->dbh->prepare($sql);
if ($sth->execute($data)) {
return $sth->fetchAll();
} else {
return false;
}
}
}
Answer the question
In order to leave comments, you need to log in
Well, you didn't tell anyone in your class to display errors. Here he is silent.
Here's a normal class for you.
class Db
{
public $dbh;
public function __construct()
{
$config = (include __DIR__.'/../../config.php')['db'];
$options = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
];
$dsn = "mysql:host=$config[host];dbname=$config[dbname];charset=utf8mb4";
$this->dbh = new \PDO($dsn, $config['login'], $config['password'], $options);
}
public function query(string $sql, array $data = [])
{
$sth = $this->dbh->prepare($sql);
$sth->execute($data);
return $sth;
}
public function getAll(string $sql, array $data = [])
{
return $this->query($sql, $data)->fetchAll();
}
}
$dbh = new \App\Db();
$about = $dbh->getAll('SELECT * FROM about');
var_dump($about);die;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question