Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Emulacja klasy mysqli
Forum PHP.pl > Forum > PHP > Object-oriented programming
MicNeo
Witam,

Mam dość nietypowe pytanie - wręcz lekko głupie. Otóż, na co dzień w pracy z bazą mysql wykorzystuje mysqli. Niestety okazało się, że muszę postawić stronę na serwerze który tego moduły nie ma (!). No i klapa, zbyt dużo zmian by ręcznie to zmieniać.

Posiadam klasa dbmanager która rozszerza mysqli. Oczywiście, pluje się serwer o to, że nie może znaleźć klasy mysqli. Mam plan 'po prostu' napisać własną klasę 'emulującą' mysqli. No i tutaj moje pytanie, czy nie spotkaliście się już z gotowcem? Dużo by mi to ułatwiło prace.

Pozdrawiam!

EDIT: Zmieniłem nazwę tematu, bo chyba niektórzy mnie nie zrozumieli smile.gif
micha12344
nie wiem ale może takie cuś snitch.gif
http://www.phpclasses.org/browse/file/2143.html
MicNeo
Nie to mi chodziło smile.gif Chodziło mi o coś takiego:
  1. <?php
  2. /*
  3. mysqli.so-OOP-Hack v0.1
  4. Use MySQLi Object oriented style as installed MySQLi module on server or local.
  5.   Copyright (C) 2008, Barış Yüksel { brsyuksel.com }
  6.  
  7.   This program is free software: you can redistribute it and/or modify
  8.   it under the terms of the GNU General Public License as published by
  9.   the Free Software Foundation, either version 3 of the License, or
  10.   (at your option) any later version.
  11.  
  12.   This program is distributed in the hope that it will be useful,
  13.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15.   GNU General Public License for more details.
  16.  
  17.   You should have received a copy of the GNU General Public License
  18.   along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. class mysqli{
  21. public $num_rows,$lengths,$error,$errno,$insert_id,$thread_id;
  22. private $mysql,$result;
  23.  
  24. const MYSQLI_BOTH = 'MYSQL_BOTH';
  25. const MYSQLI_NUM = 'MYSQL_NUM';
  26. const MYSQLI_ASSOC = 'MYSQL_ASSOC';
  27.  
  28. public function __construct($host,$username,$pass,$dbname){
  29. try{
  30. $this->mysql = mysql_connect($host,$username,$pass);
  31. if (!$this->mysql){
  32. throw new Exception(mysql_errno() . " : " . mysql_error());
  33. }
  34. else{
  35. mysql_select_db($dbname,$this->mysql);
  36. }
  37. }
  38. catch(Exception $error){
  39. echo $error->getMessage();
  40. }
  41. }
  42.  
  43. protected function refresh_properties(){
  44. $this->thread_id = @mysql_thread_id($this->mysql);
  45. $this->insert_id = @mysql_insert_id($this->mysql);
  46. $this->error = @mysql_error($this->mysql);
  47. $this->errno = @mysql_errno($this->mysql);
  48. $this->affected_rows = @mysql_affected_rows($this->mysql);
  49. $this->num_rows = @mysql_num_rows($this->result);
  50. $this->lengths = @mysql_fetch_lengths($this->result);
  51. }
  52.  
  53. public function query($query){
  54. $this->result = mysql_query($query,$this->mysql);
  55. self::refresh_properties();
  56. return $this;
  57. }
  58.  
  59. public function select_db($dbname){
  60. $event = mysql_select_db($dbname,$this->mysql);
  61. self::refresh_properties();
  62. return $event;
  63.  
  64. }
  65.  
  66. public function fetch_assoc(){
  67. return mysql_fetch_assoc($this->result);
  68. }
  69.  
  70. public function fetch_array($flag = MYSQLI_BOTH){
  71. return mysql_fetch_array($this->result,$flag);
  72. }
  73.  
  74. public function fetch_row(){
  75. return mysql_fetch_row($this->result);
  76. }
  77.  
  78. public function fetch_object(){
  79. if (func_num_args() == 0){
  80. return mysql_fetch_object($this->result);
  81. }
  82. elseif (func_num_args() == 1){
  83. $classname = func_get_arg(0);
  84. return mysql_fetch_object($this->result,$classname);
  85. }
  86. elseif (func_num_args() > 1){
  87. $args = func_get_args();
  88. $classname = func_get_arg(0);
  89. $params = array_splice($args,1);
  90. return mysql_fetch_object($this->result,$classname,$params);
  91. }
  92. }
  93.  
  94. public function fetch_field(){
  95. return mysql_fetch_field($this->mysql,0);
  96. }
  97.  
  98. public function data_seek($data_seek){
  99. return mysql_data_seek($this->result,$data_seek);
  100. }
  101.  
  102. public function field_seek($field_seek){
  103. return mysql_field_seek($this->result,$field_seek);
  104. }
  105.  
  106. public function free_result(){
  107. return mysql_free_result($this->result);
  108. }
  109.  
  110. public static function get_client_info(){
  111. }
  112.  
  113. public function ping(){
  114. return mysql_ping($this->mysql);
  115. }
  116.  
  117. public function change_user($username,$pass,$dbname){
  118. return mysql_change_user($username,$pass,$dbname,$this->mysql);
  119. }
  120.  
  121. public function real_escape_string($escapestr){
  122. return mysql_real_escape_string($escapestr,$this->mysql);
  123. }
  124.  
  125. public function escape_string($escapestr){
  126. return mysql_real_escape_string($escapestr,$this->mysql);
  127. }
  128.  
  129. public function set_charset($charset){
  130. return mysql_set_charset($charset,$this->mysql);
  131. }
  132.  
  133. public function stat(){
  134. return mysql_stat($this->mysql);
  135. }
  136.  
  137. public function close(){
  138. return mysql_close($this->mysql);
  139. }
  140. }
  141. ?>


Jak pisałem tego posta i użyłem słowa 'emulować' to wpadłem na pomysł jak szukać tej piekielnej klasy biggrin.gif Właśnie ją testuje, ale wygląda obiecująco.
MicNeo
To nie ma być obsługa mysql tylko mysqli - a dokładniej emulacja takowej klasy przy niezainstalowanym module mysqli. O to pytałem panowie, czytamy ze zrozumieniem.

Pozdrawiam!

EDIT: Musze powiedzieć, że jak narazie klasa jest bardzo dobra. Tylko w dwóch miejscach wypluwa błędy (mimio to działa)
  1. return mysql_close($this->mysql);
  2.  
  3. i
  4.  
  5. $this->result = mysql_query($query,$this->mysql);
  6.  


Pluje się o $this-link. Dałem '@' i śmiga. Na czas przeprowadzki na nowy serwer powinno wystarczyć.
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-2024 Invision Power Services, Inc.