I
I
Ivan Ivanovich2019-12-03 16:16:31
PHP
Ivan Ivanovich, 2019-12-03 16:16:31

How to get WHERE pages correctly?

Hello.
There is an abstract class whose code is:
...

public static function updateViews(): array{
        $db = Db::getInstance();
        return $db->query('UPDATE `' . static::getTableName() . '` SET `views` = `views` +1', [], static::class);
    }
public static function getByUrl(string $url): ?self{
  $db = Db::getInstance();
  $entities = $db->query(
'SELECT * FROM `' . static::getTableName() . '` WHERE url=:url' ,
[':url' => $url],
static::class
);
return $entities ? $entities[0] : null;
}

...
i.e. here are 2 functions, the first is to increase views by one and the other is to get the url.
Controller
...
public function view(string $articleUrl){
      $article = Article::getByUrl($articleUrl);
      $updateViews = Article::updateViews();

...
And I don't understand how I can make the WHERE for the updateViews function dynamic.
Those. i did it for testing
return $db->query('UPDATE `' . static::getTableName() . '` SET `views` = `views` +1 WHERE id = 1', [], static::class);

Everything works, but how can I dynamically substitute here the url that we get in the getByUrl function?
Tell me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2019-12-03
@IwanQ

You would not climb into abstract classes yet.
And if on the topic, then after all, you have already been explained 5 times how to use your own database class

$sql = 'UPDATE `' . static::getTableName() . '` SET `views` = `views` +1 WHERE id=?';
return $db->query($sql, [$article->id]);

It is necessary to substitute not the url, but the id found above

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question