Witam!

Od paru dni uczę się OOP.
Czy takie połączenie/wykonannie zapytania jest poprawne/bezpieczne?
Jeśli nie to co zmienić?

Pozdrawiam

  1. <?php
  2.  
  3. ## REQUIRE LIBS ##
  4.  
  5. require('libs/php_class/database/dbMysql.class.php');
  6. require('libs/php_smarty/Smarty.class.php');
  7.  
  8. ## CLASS ##
  9.  
  10. $smarty = new Smarty;
  11. $pdo = new dbMySQL;
  12.  
  13. ## SMARTY OPTIONS COMPILE ##
  14.  
  15. //$smarty->force_compile = true;
  16. //$smarty->debugging = true;
  17. $smarty->caching = true;
  18. $smarty->cache_lifetime = 120;
  19.  
  20. ## DATA ##
  21.  
  22. $stmt = $pdo->query('SELECT title FROM news LIMIT 5');
  23.  
  24. while($row = $stmt->fetch())
  25. {
  26. $data[] = $row;
  27. }
  28.  
  29. $stmt->closeCursor();
  30.  
  31. unset($stmt);
  32.  
  33. ## SMARTY ASSIGN ##
  34.  
  35. $smarty -> assign('data', $data);
  36.  
  37.  
  38. ## DISPLAY TEMPLATE ##
  39.  
  40. $smarty->display('templates/public/index.tpl');


  1. <?php
  2.  
  3. class dbMySQL extends PDO {
  4.  
  5. private $db_name = 'ac_2';
  6. private $db_user = 'root';
  7. private $db_pass = '';
  8. private $db_host = 'localhost';
  9.  
  10. ## CONNECT DATABASE ##
  11.  
  12. public function __construct() {
  13.  
  14. try
  15. {
  16. parent::__construct("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user, $this->db_pass);
  17. }
  18. catch (PDOException $e)
  19. {
  20. echo 'Brak połączenia z bazą danych. '. $e->getMessage();
  21. }
  22.  
  23. }
  24.  
  25. }
  26.