Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php][mysql] Klasa do obsługi baz danych.
Maciekbjw
post
Post #1





Grupa: Zarejestrowani
Postów: 217
Pomógł: 23
Dołączył: 2.12.2007
Skąd: Warszawa

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


Witam, zacząłem pisać sobie prostą klasę do obsługi baz danych. Zawierać będzie podstawowe funkcje, czyli łączenie, rozłaczanie, dodawanie, edycję i kasowanie rekordów. Mam już początek i chciałbym spytać Was, może bardziej doświadczonych, czy idę w dobrym kierunku i czy takie pisanie jest poprawne. Byłbym wdzięczny za jakieś opinie i uwagi.

Oto kod:

Kod
class MySQL {


  private


  $type, #typ

  $host,  #host

  $user,  #uzytkownik

  $name,  #nazwa bazy

  $password, #haslo do bazy
  
  $connection; #uchwyt do polaczenia
  
  
  public
  
  $active, #uchwyt do akutalnego zapytania
  
  $time, #czas wykonania zapytan
  
  $count, #ilosc zapytan
  
  $error; #ewentualne bledy
  
  
  #konstruktor klasy MySQL
function __construct() {


require_once('db-config.php')
or die('Error, file not found!');

$this->type = $type;
$this->host = $host;
$this->user = $user;
$this->name = $name;
$this->password = $password;
$this->count = 0;

if(!isset($this->connection)) {
$this->Connect();

}
}
#laczy sie z baza danych
function Connect() {
$this->connection = mysql_connect('$this->host','$this->name','$this->user','$this->password')
or die ('Error!');


}
#rozlacza sie z baza danych
function Disconnect() {


mysql_close($this->connection);

}
#w razie niepowodzenia wywala blad
function Error(){

$this->error = mysql_error();


}
} #end class
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
matix
post
Post #2





Grupa: Zarejestrowani
Postów: 278
Pomógł: 10
Dołączył: 13.02.2007
Skąd: Rybnik

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


Przede wszystkim jeśli tworzysz klase do obsługi bazy danych to tylko po to, żeby sobie uprościć zapytania, zaoszczędzić tego żmudnego łączenia itp.

Przedstawiam moją klasę bazy danych. Piszę na niej od roku i jest wszystko gitara (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

  1. <?
  2. class db {
  3. private $_Conn, $_rLast, $_sLast, $_aAttributes, $_sMethod;
  4.  
  5. public function __construct($connection = '')
  6. {
  7. $oConfig = new config('database');
  8.  
  9. $sDefault = $oConfig->default_connection;
  10. $sConnection = $connection != '' ? $connection : $sDefault;
  11.  
  12. $aSource = $oConfig->$sConnection;
  13.  
  14. $sSource = sprintf('%s:host=%s;dbname=%s',
  15. $aSource['type'],
  16. $aSource['host'],
  17. $aSource['database']
  18. );
  19.  
  20. $this->_Conn = new PDO($sSource, $aSource['user'], $aSource['pass']);
  21.  
  22. }
  23.  
  24. public function setResult($method)
  25. {
  26. $this->_sMethod = $method;
  27. }
  28.  
  29. public function update($table)
  30. {
  31. $iCountParams = count($this->_aAttributes);
  32. $iParam = 0;
  33.  
  34. if($iCountParams)
  35. {
  36. $sQuery = "UPDATE `" . $table . "` SET ";
  37.  
  38. foreach($this->_aAttributes as $sParam => $mValue)
  39. {
  40. $iParam++;
  41. $sQuery .= "" . $sParam . " = '" . $mValue . "'";
  42.  
  43. if($iParam < $iCountParams)
  44. $sQuery .= ", ";
  45. }
  46.  
  47. if($this->_sWhere)
  48. $sQuery .= " WHERE " . $this->_sWhere;
  49.  
  50. $this->_sLast = $sQuery;
  51. }
  52.  
  53. return $this;
  54. }
  55.  
  56. public function assign(array $aNewParams, $bSetValues = TRUE)
  57. {
  58. if(!is_bool($bSetValues))
  59. throw new exception('$bSetValues must be boolean!');
  60.  
  61. if($bSetValues === TRUE)
  62. {
  63. foreach($aNewParams as $sParam => $mValue)
  64. {
  65. $this->_aAttributes[$sParam] = $mValue;
  66. }
  67. }
  68. else
  69. {
  70. foreach($aNewParams as $sParam)
  71. {
  72. $this->_aAttributes[$sParam] = NULL;
  73. }
  74. }
  75.  
  76. return $this;
  77. }
  78.  
  79. public function execute($query = '')
  80. {
  81. $query = $query == '' ? $this->_sLast : $query;
  82.  
  83. $this->_rLast = $this->_Conn->query($query, $this->_sMethod);
  84.  
  85. return $this;
  86. }
  87.  
  88. public function get($table)
  89. {
  90. $this->_sLast = 'SELECT * FROM `'.$table.'`';
  91.  
  92. return $this;
  93. }
  94.  
  95. public function insert($table)
  96. {
  97. $sKeys = implode(' , ', array_keys($this->_aAttributes));
  98. $sValues = implode('" , "', array_values($this->_aAttributes));
  99.  
  100. $this->_sLast = ('insert into '.$table.' ('.$sKeys.') VALUES ("'.$sValues.'")');
  101.  
  102. return $this;
  103. }
  104.  
  105. public function delete($table)
  106. {
  107. $this->_sLast = 'DELETE FROM `'.$table.'`';
  108.  
  109. return $this;
  110. }
  111.  
  112. public function where($what, $where)
  113. {
  114. $this->_sLast .= ' WHERE '.$what.' = "'.$where.'"';
  115.  
  116. return $this;
  117. }
  118.  
  119. public function count($what)
  120. {
  121. $this->_sLast = 'SELECT COUNT(*) as count FROM '.$what;
  122.  
  123. return $this;
  124. }
  125.  
  126. public function query()
  127. {
  128. return $this->_sLast;
  129. }
  130.  
  131. public function limit($ile, $max)
  132. {
  133. $this->_sLast .= ' LIMIT '.$ile.', '.$max;
  134.  
  135. return $this;
  136. }
  137.  
  138. public function order($what, $type = 'asc')
  139. {
  140. $this->_sLast .= ' ORDER BY '.$what.' '.$type;
  141.  
  142. return $this;
  143. }
  144.  
  145. public function fetchAll()
  146. {
  147. foreach ($this->fetch() as $Row)
  148. $Rows [] = $Row;
  149.  
  150. return $Rows;
  151. }
  152.  
  153. public function fetchOne()
  154. {
  155. $aRows = $this->fetchAll();
  156. return $aRows[0];
  157. }
  158.  
  159. public function fetchRow($row)
  160. {
  161. $aRow = $this->fetchOne();
  162.  
  163. return $aRow[$row];
  164. }
  165.  
  166. public function fetch()
  167. {
  168. if (eregi('SELECT COUNT', $this->_sLast)):
  169. $rResult = $this->_rLast;
  170.  
  171. foreach ($rResult as $sRow)
  172. return $sRow['count'];
  173. endif;
  174.  
  175. return $this->_rLast;
  176. }
  177.  
  178. }
  179. ?>


Przykłady wykorzystania mogę napisać na PW osobom które będą zainteresowane, choć myślę, że klasa nie ma zbyt wiele innowacji i każdy średniozaawansowany programista php da sobie radę z wykorzystaniem (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
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: 27.12.2025 - 14:31