Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Klasowo wyjątkowy problem
Forum PHP.pl > Forum > PHP
barthezpl
Witam. Chciałbym się Was zapytać o problem związany z klasami jak również z wyjątkami.
Otóż,mam trzy pliki
main.class.php:
  1. <?php
  2. include_once("ConnectException.class.php");
  3. include_once("QueryException.class.php");
  4. error_reporting(E_ALL - E_WARNING);
  5.  
  6. class DAL
  7. {
  8. public $connection;
  9. public $selectBD;
  10. public $result;
  11. public function connect($con)
  12. {
  13. $this->connection = mysql_connect($con);
  14. $this->selectDB = mysql_select_db('zlecenia');
  15. if ($this->connection==true)
  16. {
  17. echo "POŁACZENIE POWIODŁO SIE";
  18. }
  19. if ($this->connection==false)
  20. {
  21. throw new ConnectException($this->connection);
  22. }
  23.  
  24. }
  25. public function selectDataBase($base)
  26. {
  27. $this->selectDB = mysql_select_db($base);
  28.  
  29. }
  30. public function execute($query)
  31. {
  32. $this->result = mysql_query($this->connection, $query);
  33. if(!is_resource($this->result))
  34. {
  35. throw new QueryException($this->connection);
  36. }
  37. }
  38.  
  39. }
  40. $db = new DAL();
  41. try
  42. {
  43. $db->connect('xxx' , 'xxx' , 'xxx');
  44. $db->selectDataBase('zlecenia');
  45. try
  46. {
  47. $db->execute("select * from Uzytkownicy");
  48. }
  49. catch (Exception $queryexception)
  50. {
  51. echo $queryexception->getMessage();
  52. }
  53. }
  54. catch(Exception $connectexception)
  55. {
  56. echo $connectexception->getMessage();
  57. }
  58.  
  59. ?>


Drugi plik ConnectException.class.php:
  1. <?php
  2. class ConnectException extends Exception
  3. {
  4. public function __construct()
  5. {
  6. $message = "BŁAD POŁACZENIA Z DB";
  7. parent::__construct($message, 0000);
  8.  
  9. }
  10. }
  11. ?>


Trzeci plik QueryException.class.php:
  1. <?php
  2. class QueryException extends Exception
  3. {
  4. public function __construct($connection)
  5. {
  6. $message = "BŁĄD ZAPYTANIA";
  7. parent::__construct(mysql_error ($connection), 0);
  8.  
  9. }
  10. }
  11.  
  12. ?>


W tym momencie po wywołaniu main dostaję Exception "BŁAD POŁACZENIA Z DB" ? Dlaczego?

Gdy dokonam zmiany w main.class.php na:
  1. <?php
  2. include_once("ConnectException.class.php");
  3. include_once("QueryException.class.php");
  4. error_reporting(E_ALL - E_WARNING);
  5.  
  6. class DAL
  7. {
  8. public $connection;
  9. public $selectBD;
  10. public $result;
  11. public function connect()
  12. {
  13. $this->connection = mysql_connect('xx' , 'xx' , 'xx');
  14. $this->selectDB = mysql_select_db('zlecenia');
  15. if ($this->connection==true)
  16. {
  17. echo "POŁACZENIE POWIODŁO SIE";
  18. }
  19. if ($this->connection==false)
  20. {
  21. throw new ConnectException($this->connection);
  22. }
  23.  
  24. }
  25. public function selectDataBase($base)
  26. {
  27. $this->selectDB = mysql_select_db($base);
  28.  
  29. }
  30. public function execute($query)
  31. {
  32. $this->result = mysql_query($this->connection, $query);
  33. if(!is_resource($this->result))
  34. {
  35. throw new QueryException($this->connection);
  36. }
  37. }
  38.  
  39.  
  40. }
  41. $db = new DAL();
  42. try
  43. {
  44. $db->connect();
  45. $db->selectDataBase('zlecenia');
  46. try{
  47. $db->execute("select * from Uzytkownicy123");
  48. }
  49. catch (Exception $queryexception)
  50. {
  51. echo $queryexception->getMessage();
  52. }
  53. }
  54. catch(Exception $connectexception)
  55. {
  56. echo $connectexception->getMessage();
  57. }
  58. ?>


Dostaję zwrot" Połączenie powiodło się" lecz niestety nie wyskoczył wyjątek odnośnie zapytania(nie mam tabeli Uzytkownicy123). Dlaczego ten wyjątek nie jest wyświetlany?
cojack
  1. try {
  2. // kod
  3. } catch(ConnectException $e) {
  4. echo $e->getMessage();
  5. } catch(QueryException $e) {
  6. echo $e->getMessage();
  7. }


blokowo to po 1.

Ja nie widzę zmian które robisz, zaznacz je bo nie chce mnie się w je wnikać.
barthezpl
Zamiana w pliku main.class.php powoduje takie dziwne problemy
Cytat(barthezpl @ 7.11.2011, 10:31:36 ) *
  1. public function connect($con)
  2. {
  3. $this->connection = mysql_connect($con);
  4.  
  5.  
  6. $db = new DAL();
  7. try
  8. {
  9. $db->connect('xxx' , 'xxx' , 'xxx');


  1. public function connect()
  2. {
  3. $this->connection = mysql_connect('xx' , 'xx' , 'xx');
  4.  
  5. $db = new DAL();
  6. try
  7. {
  8. $db->connect();
  9.  
nospor
Drobne poprawki:
  1. public function execute($query)
  2. {
  3. $this->result = mysql_query($this->connection, $query);
  4. if(!is_resource($this->result))
  5. {
  6. echo 1;
  7. throw new QueryException($this->connection);
  8. }
  9. }

  1. class QueryException extends Exception
  2. {
  3. public function __construct($connection)
  4. {
  5. $message = "BŁĄD ZAPYTANIA";
  6. echo 2;
  7. parent::__construct($message, 0);
  8.  
  9. }
  10. }

Co to wyświetla?

ps:
E_ALL - E_WARNING
Co to niby ma być? Zastanów się jednak zanim odpowiesz na to pytanie wink.gif

To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.