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%)
-----


@Xniver:

A na czym jest oparta ta klasa? (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
Creole? Proszę bardzo. Mówisz masz - też mam taką klase (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

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


Pozdro =)
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: 4.10.2025 - 15:43