Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Uzywanie database w innej klasie
goartur
post
Post #1





Grupa: Zarejestrowani
Postów: 233
Pomógł: 27
Dołączył: 19.10.2014

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


Czesc mam problem z uzywaniem mysql w innej klasie, jestem poczatkujacy w oop, lecz chce wysylac zapytania z klasy user_Access lecz niedziala funkcja $this->db_query, dotaje meldunek "Query method not found in class", probowalem tez uzywc: mysqli_query, lecz tez niedziala,
mam taka klase do polaczenia a mysql
  1. class db
  2. {
  3. public function db()
  4. {
  5. $db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
  6.  
  7.  
  8. if ($db->connect_errno) {
  9.  
  10. echo "Error: Could not connect to database.";
  11.  
  12.  
  13. }
  14. else { return $db; }
  15.  
  16. }
  17.  
  18.  
  19. }



a oto user_Access.php

  1. $db = new db();
  2. $userDB = new user_access($db->db());
  3. $userDB->check_login('artur','ol');
  4. class user_access{
  5.  
  6. public $db;
  7.  
  8. public function __construct($db){
  9. $this->db = $db;
  10.  
  11. }
  12. public function check_login($login,$password){
  13. $password = sha1($password);
  14. $sql = "SELECT * FROM admins WHERE username = '$login' AND password = '$password'";
  15. $result = $result = $this->db->query($sql);
  16.  
  17. if($result->num_rows > 1){
  18. echo 'ok';
  19. }else{
  20. echo "not";
  21. }
  22. }
  23.  
  24. }


Moze mi ktos pokazac gdzie jest blad?

Ten post edytował goartur 27.06.2015, 10:19:39
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 11)
prz3kus
post
Post #2





Grupa: Zarejestrowani
Postów: 260
Pomógł: 30
Dołączył: 22.01.2007

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


  1. class user_access{
  2.  
  3. public $db;
  4.  
  5. public function __construct($db){
  6. $this->db = $db;
  7.  
  8. }
  9. public function check_login($login,$password){
  10. $password = sha1($password);
  11. $sql = "SELECT * FROM admins WHERE username = '$login' AND password = '$password'";
  12. $result = $result = $this->db->query($sql);
  13.  
  14. if($result->num_rows > 1){
  15. echo 'ok';
  16. }else{
  17. echo "not";
  18. }
  19. }
  20.  
  21. }
  22.  
  23.  
  24. $db = new db();
  25. $userDB = new user_access($db->db());
  26. $userDB->check_login('artur','ol');


Masz

public $dba;

zamiast

public $db;
Go to the top of the page
+Quote Post
goartur
post
Post #3





Grupa: Zarejestrowani
Postów: 233
Pomógł: 27
Dołączył: 19.10.2014

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


Nie to nie to ...
Notice: Trying to get property of non-object in C:\xampp\htdocs\brothers.traning\profiadmin\models\login_Class.php on line 24
not
Go to the top of the page
+Quote Post
markuz
post
Post #4





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

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


  1. <?php
  2.  
  3. class Database {
  4.  
  5. private static $db;
  6.  
  7. public static function connect() {
  8. self::$db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
  9. }
  10.  
  11. public static function get() {
  12. return self::$db;
  13. }
  14.  
  15. }
  16.  
  17. class Model {
  18.  
  19. private $db;
  20.  
  21. public function __construct() {
  22. if(!Database::$db)
  23. Database::connect();
  24.  
  25. $this->db = Database::get();
  26. }
  27.  
  28. }
  29.  
  30. class UserAccessModel extends Model {
  31.  
  32. public function login($username, $password) {
  33. $password = sha1($password);
  34. $results = $this->db->query(
  35. "SELECT * FROM admins WHERE username = ? AND password = ?",
  36. array($username, $password)
  37. );
  38.  
  39. return $result->num_rows > 0;
  40. }
  41.  
  42. }
  43.  
  44. $userAccess = new UserAccessModel();
  45. if($userAccess->login('artur', 'ol'))
  46. // logowanie
  47. else
  48. // błąd logowania
  49.  
  50. ?>


--------------------
Go to the top of the page
+Quote Post
goartur
post
Post #5





Grupa: Zarejestrowani
Postów: 233
Pomógł: 27
Dołączył: 19.10.2014

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


Cytat(markuz @ 27.06.2015, 12:12:30 ) *
  1. <?php
  2.  
  3. class Database {
  4.  
  5. private static $db;
  6.  
  7. public static function connect() {
  8. self::$db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
  9. }
  10.  
  11. public static function get() {
  12. return self::$db;
  13. }
  14.  
  15. }
  16.  
  17. class Model {
  18.  
  19. private $db;
  20.  
  21. public function __construct() {
  22. if(!Database::$db)
  23. Database::connect();
  24.  
  25. $this->db = Database::get();
  26. }
  27.  
  28. }
  29.  
  30. class UserAccessModel extends Model {
  31.  
  32. public function login($username, $password) {
  33. $password = sha1($password);
  34. $results = $this->db->query(
  35. "SELECT * FROM admins WHERE username = ? AND password = ?",
  36. array($username, $password)
  37. );
  38.  
  39. return $result->num_rows > 0;
  40. }
  41.  
  42. }
  43.  
  44. $userAccess = new UserAccessModel();
  45. if($userAccess->login('artur', 'ol'))
  46. // logowanie
  47. else
  48. // błąd logowania
  49.  
  50. ?>


Dzieki za odpowiedz dostaje taki blad:

Fatal error: Cannot access private property Database::$db in C:\xampp\htdocs\brothers.traning\profiadmin\login_Class.php on line 27
Go to the top of the page
+Quote Post
markuz
post
Post #6





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

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


Ok. Błąd dotyczy tego fragmentu kodu:

  1. public function __construct() {
  2. if(!Database::$db)
  3. Database::connect();
  4.  
  5. $this->db = Database::get();
  6. }


Database::$db jest prywatne. W jaki sposób się do niego dostać? Oto zagadka dla Ciebie wink.gif Rozwiązanie znajduje się w tym samym fragmencie kodu.


--------------------
Go to the top of the page
+Quote Post
goartur
post
Post #7





Grupa: Zarejestrowani
Postów: 233
Pomógł: 27
Dołączył: 19.10.2014

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


Cytat(markuz @ 27.06.2015, 12:35:06 ) *
Ok. Błąd dotyczy tego fragmentu kodu:

  1. public function __construct() {
  2. if(!Database::$db)
  3. Database::connect();
  4.  
  5. $this->db = Database::get();
  6. }


Database::$db jest prywatne. W jaki sposób się do niego dostać? Oto zagadka dla Ciebie wink.gif Rozwiązanie znajduje się w tym samym fragmencie kodu.

Musze zmienc private $db na public smile.gif?, jesli tak to dostaje taki blad:

  1. Warning: mysqli::query() expects parameter 2 to be long, array given in C:\xampp\htdocs\brothers.traning\profiadmin\login_Class.php on line 44
  2.  
  3. Notice: Undefined variable: result in C:\xampp\htdocs\brothers.traning\profiadmin\login_Class.php on line 46
  4.  
  5. Notice: Trying to get property of non-object in C:\xampp\htdocs\brothers.traning\profiadmin\login_Class.php on line 46
Go to the top of the page
+Quote Post
markuz
post
Post #8





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

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


Ogólnie powinieneś przenieść cały ten fragment w construct() do metody get w Database ale nieważne.. Możesz ustawić na public.

Co do drugiego błędu to pomyliłem mysqli z PDO. Zmień wykonywanie zapytania na :
  1. $results = $this->db->query("SELECT * FROM admins WHERE username = '$username' AND password = '$password'");


Ten post edytował markuz 27.06.2015, 11:45:35


--------------------
Go to the top of the page
+Quote Post
goartur
post
Post #9





Grupa: Zarejestrowani
Postów: 233
Pomógł: 27
Dołączył: 19.10.2014

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


Cytat(markuz @ 27.06.2015, 12:45:28 ) *
Ogólnie powinieneś przenieść cały ten fragment w construct() do metody get w Database ale nieważne.. Możesz ustawić na public.

Co do drugiego błędu to pomyliłem mysqli z PDO. Zmień wykonywanie zapytania na :
  1. $results = $this->db->query("SELECT * FROM admins WHERE username = '$username' AND password = '$password'");



Nadal ten sam blad:

  1. Notice: Undefined variable: result in C:\xampp\htdocs\brothers.traning\profiadmin\login_Class.php on line 43
  2.  
  3. Notice: Trying to get property of non-object in C:\xampp\htdocs\brothers.traning\profiadmin\login_Class.php on line 43
Go to the top of the page
+Quote Post
markuz
post
Post #10





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

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


Tym razem wkradła się literówka (results, result). Usiądź nad kodem trochę samemu i spróbuj znaleźć błąd inaczej będzie Ci ciężko cokolwiek napisać..


--------------------
Go to the top of the page
+Quote Post
goartur
post
Post #11





Grupa: Zarejestrowani
Postów: 233
Pomógł: 27
Dołączył: 19.10.2014

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


Cytat(markuz @ 27.06.2015, 13:10:27 ) *
Tym razem wkradła się literówka (results, result). Usiądź nad kodem trochę samemu i spróbuj znaleźć błąd inaczej będzie Ci ciężko cokolwiek napisać..



Wiem widze zauwazylem to,

  1. Notice: Trying to get property of non-object in C:\xampp\htdocs\brothers.traning\profiadmin\login_Class.php on line 43

Zawsze mam ten sam problem i nikt mi nie moze powiedziec o co w tym chodz ...
Go to the top of the page
+Quote Post
viking
post
Post #12





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


Dostałeś odpowiedź a i kod błędu wszystko mówi. Aby ->metoda() zadziałała zmienna musi być klasą. var_dump($zmienna) i zobaczysz gdzie jest błąd. Będzie tam pewnie null. Private ma zasięg tylko w Model, zmień na protected.


--------------------
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 Aktualny czas: 20.08.2025 - 12:02