Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]PDO - Problem z funkcją PREPARE
Panicz74
post 10.12.2015, 07:18:43
Post #1





Grupa: Zarejestrowani
Postów: 39
Pomógł: 1
Dołączył: 28.08.2015

Ostrzeżenie: (0%)
-----


Witam ponownie,

Korzystam z pewnego kursu i napisałem taki kod zgodnie ze wskazówkami. Niestety otrzymuje komunikat: Połączono
Fatal error: Call to a member function prepare() on null in C:\xampp\htdocs\eclipse\app\classes\DB.php on line 39


DB.php:
  1. <?php
  2. class DB
  3. {
  4. private static $_instance = null;
  5. private $_pdo,
  6. $_query,
  7. $_error = false,
  8. $_results,
  9. $_count = 0;
  10.  
  11. private function __construct()
  12. {
  13. try
  14. {
  15. $this->pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'),
  16. Config::get('mysql/username'),
  17. Config::get('mysql/password'));
  18. echo 'Połączono';
  19. }
  20. catch(PDOException $e)
  21. {
  22. die($e->getMessage());
  23. }
  24.  
  25. }
  26.  
  27. public static function getInstance()
  28. {
  29. if(!isset(self::$_instance))
  30. {
  31. self::$_instance = new DB();
  32. }
  33. return self::$_instance;
  34. }
  35.  
  36. public function query($sql, $params = array())
  37. {
  38. $this->_error = false;
  39. if($this->_query = $this->_pdo->prepare($sql))
  40. {
  41. echo 'Success';
  42. }
  43. }
  44. }
  45.  
  46. ?>


index.php:
  1. <?php
  2. require_once 'core/init.php';
  3.  
  4. DB::getInstance()->query("SELECT username FROM users WHERE username");
  5.  
  6. ?>


Gdzie popełniam błąd?

Ten post edytował Panicz74 10.12.2015, 07:19:59
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
KsaR
post 10.12.2015, 07:25:55
Post #2





Grupa: Zarejestrowani
Postów: 520
Pomógł: 102
Dołączył: 15.07.2014
Skąd: NULL

Ostrzeżenie: (0%)
-----


private $_pdo
(...)
$this->pdo = new PDO
(...)
$this->_pdo->prepare($sql)

Własności inne używasz.

Ten post edytował KsaR 10.12.2015, 07:27:49


--------------------
Go to the top of the page
+Quote Post
Panicz74
post 10.12.2015, 07:27:37
Post #3





Grupa: Zarejestrowani
Postów: 39
Pomógł: 1
Dołączył: 28.08.2015

Ostrzeżenie: (0%)
-----


Śmiga, dzięki wielkie smile.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 12:27