Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [mysql] klasa do obsługi MySQL
mynio
post
Post #1





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 25.03.2005

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


witam
napisalem sobie prostą klasę do obsługi MySQL, nie zamieszczam tego jeszcze w dziale oceny ponieważ to nic wielkiego, chciałbym jeszcze nad nią tochę popracować, co radzicie, przeglądałem troche kody podobnych klas, czekam na Wasze opinie
myślę o jakiejś obsłudze błędów ponieważ narazie nie ma to żadnych zabezpieczeń
pzdr

  1. <?
  2. //show_source ('mysql.class.php');
  3. error_reporting (E_ALL);
  4.  
  5. class mysql {
  6.  
  7. private $host;
  8. private $user;
  9. private $password;
  10. private $database;
  11. public $id_connection;
  12.  
  13.  public function __construct ($host, $user, $password, $database) {
  14.  
  15.  $this->host = $host;
  16.  $this->user = $user;
  17.  $this->password = $password;
  18.  $this->database = $database;
  19.  
  20. if (! ($this->id_connection = mysql_connect ($this->host, $this->user, $this->password))) {
  21. throw new Exception ('Nie mogę połączyć się z serwerem bazy danych');
  22.  } 
  23. if (! (mysql_select_db($this->database))) {
  24. throw new Exception ('Nie mogę połączyć się bazą danych: '.$this->database);
  25. } 
  26.  
  27. }
  28.  
  29. public function make_query($sql) {
  30. return new mysql_res (mysql_query($sql, $this->id_connection));
  31. }
  32.  
  33. public function __destruct () {
  34. mysql_close ($this->id_connection);
  35. }
  36. }
  37.  
  38. class mysql_res implements IteratorAggregate {
  39.  
  40. private $result;
  41.  
  42. public function __construct($result) {
  43. $this->result = $result;
  44. }
  45.  
  46. public function getIterator() {
  47. return new mysql_res_iterator($this->result);
  48. }
  49.  
  50.  
  51. }
  52.  
  53. class mysql_res_iterator implements Iterator {
  54.  
  55. private $result;
  56. private $current = 0;
  57. private $rows;
  58.  
  59. public function __construct($result) {
  60. $this->result = $result;
  61. $this->rows = mysql_num_rows($this->result);
  62. }
  63.  
  64. public function current() {
  65. return mysql_fetch_object($this->result);
  66. }
  67.  
  68. public function key() {
  69. return $this->current;
  70. }
  71.  
  72. public function rewind() {
  73. $this->current = 0;
  74. mysql_data_seek($this->result, $this->current);
  75. }
  76.  
  77. public function next() {
  78. $this->current++;
  79. }
  80.  
  81. public function valid() {
  82. return $this->current < $this->rows;
  83. }
  84.  
  85. }
  86. ?>


i użycie:

  1. <?
  2.  
  3. function __autoload($classname) {
  4. include_once $classname.'.class.php';
  5. }
  6.  
  7.  
  8. try {
  9. $db = new mysql('localhost','root','krasnal', 'tomek');
  10. $sql = 'SELECT * FROM ks_tel';
  11. $dane = $db->make_query($sql);
  12.  
  13. foreach ($dane as $id ){
  14. echo 'Imie: '.$id->imie.'<br>';
  15. echo 'Nazwisko: '.$id->nazwisko.'<br>';
  16. echo 'Numer: '.$id->numer.'<br><br>';
  17. }
  18.  
  19. } catch (Exception $e) {
  20. echo $e;
  21. }
  22. ?>


Ten post edytował mynio 22.08.2005, 21:43:07
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 23.08.2025 - 00:07