K
K
Kristina87872020-05-21 13:36:11
PHP
Kristina8787, 2020-05-21 13:36:11

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;


here is the Db class itself from which the database query method is taken

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;
        }

  }
}


gives false and that's it, for the life of me, I wrote the same code before in another mini-project - everything worked fine, help me find where I'm so stupid ..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-05-21
@FanatPHP

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 is made public. lastInsertId() where are you going to get it from?
- added enabling error reporting
- added support for LIMIT
- added setting encoding, so that later there would be no questions "Oh, I have bugs!!"
- the meaningless execute function was removed
- the query function was made meaningful and universal
- the $data parameter was made optional
- a function was added to get an array of strings
And remember - you need to create it once for the entire application.
$dbh = new \App\Db();
$about = $dbh->getAll('SELECT * FROM about');
var_dump($about);die;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question