Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [skrypt][OBIEKT] Interpreter PDO
Posio
post
Post #1





Grupa: Zarejestrowani
Postów: 417
Pomógł: 44
Dołączył: 23.06.2011

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


Chciałbym poddać ocenie napisany dziś obiekt, który ma mi ułatwić i przyśpieszyć akcje związane z bazą danych.
Podkreślam ,że dopiero rozpoczynam przygodę z programowaniem obiektowym.

Głównie chodzi mi o ocenę wydajnośći skryptu. - Nie wiem czy lepiej być stale połączonym z bazą czy co chwile otwierać nowe połączenia(tak jak w klasie).
W kodzie może być malutki bałagan ale wszyscy wiemy jak coś wygląda gdy robi się to po dniu pracy.

Funkcje:
-insert
-update
-select

Wszystko POWINNO chodzić sprawnie.


  1. class dataBase {
  2.  
  3. private $_host = null;
  4. private $_database = null;
  5. private $_username = null;
  6. private $_password = null;
  7.  
  8. public function __construct(){
  9. $nn = new Hconfig;
  10. $data = $nn->dbConf();
  11. $this->_host = $data['host'];
  12. $this->_database = $data['db'];
  13. $this->_username = $data['username'];
  14. $this->_password = $data['password'];
  15.  
  16. }
  17.  
  18. public function insert($table, $fields, $values, $params){
  19. $table = '`'.$table.'`';
  20. $fields = preg_replace('/\s+/', '', $fields);
  21. $fields = explode(',', $fields);
  22. $values = explode(',', $values);
  23. $cf = count($fields);
  24. $cv = count($values);
  25. if($cf != $cv) { echo 'Podano złe parametry';}
  26. for($i=0;$i<$cf;$i++){$qf .= '`'.$fields[$i].'`, ';}
  27. $qf = substr($qf,0,-2);
  28. for($i=0;$i<$cv;$i++){$vf .= ':'.$fields[$i].', ';}
  29. $vf = substr($vf,0,-2);
  30. $vfe = preg_replace('/\s+/', '', explode(',', $vf));
  31. $params = explode(',', $params);
  32. try{
  33. $pdo = new PDO('mysql:host='.$this->_host.';dbname='.$this->_database.'', ''.$this->_username.'', ''.$this->_password.'');
  34. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  35. $stmt = $pdo->prepare('INSERT INTO '.$table.' ('.$qf.') VALUES ('.$vf.')');
  36. for($i=0;$i<$cf;$i++) {
  37. $stmt->bindValue($vfe[$i], $values[$i], $params[$i]);
  38. }
  39. $status = $stmt->execute();
  40. return $status;
  41. $stmt -> closeCursor();
  42. unset($stmt);
  43. unset($pdo);
  44. }
  45. catch(PDOException $e){
  46. echo $e->getMessage();
  47. }
  48. }
  49.  
  50. public function update($table, $fields, $values, $params, $where){
  51. if(empty($where)) {$wh = '';} else {$wh = 'WHERE ('.$where.')';};
  52. $table = '`'.$table.'`';
  53. $fields = preg_replace('/\s+/', '', $fields);
  54. $fields = explode(',', $fields);
  55. $values = explode(',', $values);
  56. $cf = count($fields);
  57. $cv = count($values);
  58. if($cf != $cv) { echo 'Podano złe parametry';}
  59. for($i=0;$i<$cf;$i++){$qf .= '`'.$fields[$i].'`, ';}
  60. $qf = substr($qf,0,-2);
  61. for($i=0;$i<$cv;$i++){$vf .= ':'.$fields[$i].', ';}
  62. $vf = substr($vf,0,-2);
  63. $vfe = preg_replace('/\s+/', '', explode(',', $vf));
  64. for($i=0;$i<$cf;$i++){$ustring .= '`'.$fields[$i].'`='.$vfe[$i].', ';}
  65. $ustring = substr($ustring,0,-2);
  66. $params = explode(',', $params);
  67. try{
  68. $pdo = new PDO('mysql:host='.$this->_host.';dbname='.$this->_database.'', ''.$this->_username.'', ''.$this->_password.'');
  69. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  70. $stmt = $pdo->prepare('UPDATE '.$table.' SET '.$ustring.' '.$wh.'');
  71. for($i=0;$i<$cf;$i++) {
  72. $stmt->bindValue($vfe[$i], $values[$i], $params[$i]);
  73. }
  74. $status = $stmt->execute();
  75. return $status;
  76. $stmt -> closeCursor();
  77. unset($stmt);
  78. unset($pdo);
  79. }
  80. catch(PDOException $e){
  81. echo $e->getMessage();
  82. }
  83. }
  84.  
  85. public function select($from, $fields, $where, $sort){
  86. if(empty($where)) {$wh = '';} else {$wh = 'WHERE ('.$where.')';};
  87. if(empty($sort)) {$srt = '';} else {$srt = 'SORT BY ('.$sort.')';};
  88. $table = '`'.$table.'`';
  89. try{
  90. $pdo = new PDO('mysql:host='.$this->_host.';dbname='.$this->_database.'', ''.$this->_username.'', ''.$this->_password.'');
  91. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  92. $stmt = $pdo -> query('SELECT '.$fields.' FROM '.$from.' '.$wh.' '.$srt.'');
  93. return $stmt;
  94. $stmt -> closeCursor();
  95. unset($stmt);
  96. unset($pdo);
  97. }
  98. catch(PDOException $e){
  99. echo $e->getMessage();
  100. }
  101. }
  102. }


Podkreślam, że póki co w programowaniu obiektowym raczkuje... Przyjmuje na klatę całą krytykę i sugestie na które niecierpliwie czekam.


Przykład zastosowania:

  1. $posio = new dataBase;
  2. $test = $posio->insert('users', 'id, login, password, email', ', testlogin, testpassword, testmail', '1, 2, 2, 2');
  3. if($test > 0 ) { echo 'Dane zostały dodane'; }


Ten post edytował Posio 16.06.2012, 17:56:19
Go to the top of the page
+Quote Post

Posty w temacie


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: 19.08.2025 - 20:37