Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Dane do bazy MYSQL
Forum PHP.pl > Forum > Przedszkole
pomek2
Mam pytanko, gdzie wstawić w tym kodzie dane do bazy MYSQL?
Dzięki za info

  1. <?php
  2. if(! defined('BASEPATH') ){ exit('Unable to view file.'); }
  3.  
  4.  
  5. class MySQLConnection {
  6.  
  7. private $sqlHost;
  8. private $sqlUser;
  9. private $sqlPassword;
  10. private $sqlDatabase;
  11.  
  12. private $mySqlLinkIdentifier = FALSE;
  13.  
  14. public $QueryFetchArrayTemp = array();
  15.  
  16. private $numQueries = 0;
  17.  
  18. public $UsedTime = 0;
  19.  
  20. public function __construct($sqlHost, $sqlUser, $sqlPassword, $sqlDatabase = FALSE) {
  21. $this->sqlHost = $sqlHost;
  22. $this->sqlUser = $sqlUser;
  23. $this->sqlPassword = $sqlPassword;
  24. $this->sqlDatabase = $sqlDatabase;
  25. }
  26.  
  27. public function __destruct() {
  28. $this->Close();
  29. }
  30.  
  31. public function Connect() {
  32. if($this->mySqlLinkIdentifier !== FALSE) {
  33. return $this->mySqlLinkIdentifier;
  34. }
  35.  
  36. $this->mySqlLinkIdentifier = mysql_connect($this->sqlHost, $this->sqlUser, $this->sqlPassword, TRUE); // Open new link on every call
  37. if($this->mySqlLinkIdentifier === FALSE) {
  38. return FALSE;
  39. }
  40.  
  41. if($this->sqlDatabase !== FALSE) {
  42. mysql_select_db($this->sqlDatabase, $this->mySqlLinkIdentifier);
  43. }
  44.  
  45. return $this->mySqlLinkIdentifier;
  46. }
  47.  
  48. public function Close() {
  49. if($this->mySqlLinkIdentifier !== FALSE) {
  50. mysql_close($this->mySqlLinkIdentifier);
  51. $this->mySqlLinkIdentifier = FALSE;
  52. }
  53. }
  54.  
  55. public function GetLinkIdentifier() {
  56. return $this->mySqlLinkIdentifier;
  57. }
  58.  
  59. public function Query($query) {
  60. $start = microtime(true);
  61. $result = mysql_query($query, $this->GetLinkIdentifier());
  62. $this->UsedTime += microtime(true) - $start;
  63. $this->numQueries++;
  64.  
  65. if( $result === false ){
  66. die($this->GetErrorMessage());
  67. }
  68.  
  69. return $result;
  70. }
  71.  
  72. public function FreeResult($result) {
  73. }
  74.  
  75. public function FetchArray($result) {
  76. return mysql_fetch_array($result, MYSQL_ASSOC);
  77. }
  78.  
  79. public function FetchArrayAll($result){
  80. $retval = array();
  81. if($this->GetNumRows($result)) {
  82. while($row = $this->FetchArray($result)) {
  83. $retval[] = $row;
  84. }
  85. }
  86. return $retval;
  87. }
  88.  
  89. public function GetNumRows($result) {
  90. return mysql_num_rows($result);
  91. }
  92.  
  93. public function GetNumAffectedRows() {
  94. return mysql_affected_rows($this->mySqlLinkIdentifier);
  95. }
  96.  
  97.  
  98. // Helper methods
  99. public function QueryFetchArrayAll($query) {
  100. $result = $this->Query($query);
  101. if($result === FALSE) {
  102. return FALSE;
  103. }
  104.  
  105. $retval = $this->FetchArrayAll($result);
  106. $this->FreeResult($result);
  107.  
  108. return $retval;
  109. }
  110.  
  111. public function QueryFirstRow($query) {
  112. $result = $this->Query($query);
  113. if($result === FALSE) {
  114. return FALSE;
  115. }
  116.  
  117. $retval = FALSE;
  118.  
  119. $row = $this->FetchArray($result);
  120. if($row !== FALSE) {
  121. $retval = $row;
  122. }
  123.  
  124. $this->FreeResult($result);
  125.  
  126. return $retval;
  127. }
  128.  
  129. public function QueryFirstValue($query) {
  130. $row = $this->QueryFirstRow($query);
  131. if($row === FALSE) {
  132. return FALSE;
  133. }
  134.  
  135. return $row[0];
  136. }
  137.  
  138. public function GetErrorMessage() {
  139. return "SQL Error: ".mysql_error().": ";
  140. }
  141.  
  142. public function EscapeString($string) {
  143. if (is_array($string))
  144. {
  145. $str = array();
  146. foreach ($string as $key => $value)
  147. {
  148. $str[$key] = $this->EscapeString($value);
  149. }
  150.  
  151. return $str;
  152. }
  153.  
  154. return get_magic_quotes_gpc() ? $string : mysql_real_escape_string($string, $this->mySqlLinkIdentifier);
  155. }
  156.  
  157. function GetNumberOfQueries() {
  158. return $this->numQueries;
  159. }
  160.  
  161. public function BeginTransaction() {
  162. $this->Query("SET AUTOCOMMIT=0");
  163. $this->Query("BEGIN");
  164. }
  165.  
  166. public function CommitTransaction() {
  167. $this->Query("COMMIT");
  168. $this->Query("SET AUTOCOMMIT=1");
  169. }
  170.  
  171. public function RollbackTransaction() {
  172. $this->Query("ROLLBACK");
  173. $this->Query("SET AUTOCOMMIT=1");
  174. }
  175.  
  176. public function GetFoundRows() {
  177. return $this->QueryFirstValue("SELECT FOUND_ROWS()");
  178. }
  179.  
  180. public function GetLastInsertId() {
  181. return $this->QueryFirstValue("SELECT LAST_INSERT_ID()");
  182. }
  183.  
  184.  
  185. public function QueryFetchArray($query, $all = false, $useCache = true)
  186. {
  187. $tempKey = sha1($query . ($all === true ? 'all' : 'notAll'));
  188. $temp = $this->QueryFetchArrayTemp[$tempKey];
  189.  
  190. if ($temp && $useCache === true)
  191. {
  192. return unserialize($temp);
  193. }
  194. else
  195. {
  196. $queryResult = $this->Query($query);
  197. $result = $all === true ? $this->FetchArrayAll($queryResult) : $this->FetchArray($queryResult);
  198.  
  199. $this->QueryFetchArrayTemp[$tempKey] = serialize($result);
  200. return $result;
  201. }
  202. }
  203. }
  204. ?>
Turson
Robisz instancję obiektu
  1. $obiekt = new MySQLConnection('host','user' itd);

w nawiasie podajesz dane, którą są przekazane do konstruktora
  1. public function __construct($sqlHost, $sqlUser, $sqlPassword, $sqlDatabase = FALSE)
pomek2
dzieki, ale można troszkę bardziej łopatologicznie:)?
Turson
  1. $obiekt = new MySQLConnection($sqlHost, $sqlUser, $sqlPassword, $sqlDatabase = FALSE);

gdzie zamiast zmiennych $sqlHost itd. podajesz dane do bazy danych
pomek2
Jestem laikiem w kwestii php, napisze mi ktoś gotowy kod za doładowanie za 5 zł?
Turson
Do tego jest inny dział. Więc po co się bierzesz za programowanie obiektowe, jeśli nie wiesz co to jest host, user czy hasło do bazy...
Znajdź w google prostszy kod, jeśli ci zależy.
tomxx
Na końcu skryptu (ale przed znacznikiem ?>) dodaj taki kod:
  1. $o = new MySQLConnection('localhost',
  2. '/*tu wpisz nazwę użytkownika bazy danych*/',
  3. '/*tu wpisz hasło do bazy danych*/',
  4. '/*tu wpisz nazwę bazy danych*/');

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.