Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Przerobienie klasy: pg_Connect na mysqli_connect
Forum PHP.pl > Forum > Przedszkole
dopelganger
Witam,
korzystam z gotowej klasy SQL, w której jest łączenie się z bazą PostgreSQL. Próbuje przerobić tą klasę na mySqli.
Niestety po przeróbce aplikacja mi nie działa, nie pojawia się też żadne komunikat błędu. Może coś sknociłem. Poniżej wklejam oryginalny kod z PG i pod nim moja zmiana na mysqli.
Prosze o pomoc

  1. class sql { // KOD BEZ PRZERÓBEK
  2. private $result_rows; # Tablica asocjacyjna z rezultatem zapytania
  3. private $query_handle; # Uchwyt do zapytania
  4. private $link_ident; # Uchwyt do połączenia z bazą
  5.  
  6. public function __construct() {
  7. $db_username = "gobjtest";
  8. $db_password = "";
  9. $db_host = "db";
  10. $db_name = "gobjtest";
  11. $this->link_ident = pg_Connect("user='$db_username' password=
  12. '$db_password' dbname='$db_name' host='$db_host'");
  13. }
  14.  
  15. public function query($sql, $code_return_mode = 0) {
  16. $q_handle = pg_exec($this->link_ident, $sql);
  17. for ($i=0; $i<=pg_numrows($q_handle)-1; $i++) {
  18. $result = pg_fetch_array($q_handle,$i);
  19. $return_array[$i] = $result;
  20. };
  21. if (!$q_handle) {
  22. error_log("BَD SQL: $sql\n");
  23. };
  24. $this->result_rows = $return_array;
  25. if (!$q_handle) {
  26. return(1);
  27. } else {
  28. return(0); # zwraca 0, jeśli wystąpił błąd
  29. };
  30. }
  31.  
  32. public function get_result($row_num, $column_name) {
  33. return ($this->result_rows[$row_num][$column_name]);
  34. }
  35.  
  36. public function get_row_hash($row_num) {
  37. return ($this->result_rows[$row_num]);
  38. }
  39.  
  40. public function get_table_hash() {
  41. return $this->result_rows;
  42. }
  43.  
  44. public function done($close_connection = 0) {
  45. if ($close_connection) {
  46. pg_Close($this->link_ident);
  47. };
  48. }
  49. };


A poniżej moje wypociny :|

  1. class sql {
  2. private $result_rows;
  3. private $query_handle;
  4. private $link_ident;
  5.  
  6. public function __construct() {
  7. $db_username = "root";
  8. $db_password = "krasnal";
  9. $db_host = "localhost";
  10. $db_name = "test";
  11. $this->link_ident = mysqli_connect($db_host,$db_username,$db_password,$db_name);
  12. print_r($this->link_ident);
  13. }
  14.  
  15.  
  16. public function query($sql, $code_return_mode = 0) {
  17. //$q_handle = pg_exec($this->link_ident, $sql);
  18. $q_handle = mysqli_query($this->link_ident, $sql);
  19. for ($i=0; $i<=$q_handle->num_rows-1; $i++) {
  20. //$result = pg_fetch_array($q_handle,$i);
  21. $result = $q_handle->fetch_array(MYSQLI_ASSOC);
  22. $return_array[$i] = $result;
  23. };
  24. if (!$q_handle) {
  25. error_log("ERROR SQL: $sql\n");
  26. };
  27. $this->result_rows = $return_array;
  28. if (!$q_handle) {
  29. return(1);
  30. } else {
  31. return(0);
  32. };
  33. }
  34.  
  35. public function get_result($row_num, $column_name) {
  36. return ($this->result_rows[$row_num][$column_name]);
  37. }
  38.  
  39. public function get_row_hash($row_num) {
  40. return ($this->result_rows[$row_num]);
  41. }
  42.  
  43. public function get_table_hash() {
  44. return $this->result_rows;
  45. }
  46.  
  47. public function done($close_connection = 0) {
  48. if ($close_connection) {
  49. mysqli_close($this->link_ident);
  50. };
  51. }
  52. };
nospor
Błędy ci się nie wyświetlają bo nie włączyłeś ich wyświetlania.

Poza tym jesteś nie konsekwenty. Albo używaj trybu proceduralnego, albo obiektowego. NIe możesz ich mieszać, chodzi mi np. o to:

$q_handle = mysqli_query($this->link_ident, $sql); //TU UZYWASZ STYLU PROCEDURALNEGO
for ($i=0; $i<=$q_handle->num_rows-1; $i++) {
//$result = pg_fetch_array($q_handle,$i);
$result = $q_handle->fetch_array(MYSQLI_ASSOC); //A TU NI STAD NI ZOWAD PROCEDURY CI SIE ZAMIENILY NA OBIEKTY

ALbo jedno albo drugie.
dopelganger
Cytat(nospor @ 27.10.2012, 16:22:44 ) *
Błędy ci się nie wyświetlają bo nie włączyłeś ich wyświetlania.

Poza tym jesteś nie konsekwenty. Albo używaj trybu proceduralnego, albo obiektowego. NIe możesz ich mieszać, chodzi mi np. o to:

$q_handle = mysqli_query($this->link_ident, $sql); //TU UZYWASZ STYLU PROCEDURALNEGO
for ($i=0; $i<=$q_handle->num_rows-1; $i++) {
//$result = pg_fetch_array($q_handle,$i);
$result = $q_handle->fetch_array(MYSQLI_ASSOC); //A TU NI STAD NI ZOWAD PROCEDURY CI SIE ZAMIENILY NA OBIEKTY

ALbo jedno albo drugie.


poprawiłem, ale wywala mi teraz błąd dla linii > "mysqli_num_rows($q_handle)-1; $i++)" : mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in c:\usr\apache\...

  1. public function query($sql, $code_return_mode = 0) {
  2. //$q_handle = pg_exec($this->link_ident, $sql);
  3. $q_handle = mysqli_query($this->link_ident, $sql);
  4. for ($i=0; $i<=mysqli_num_rows($q_handle)-1; $i++) {
  5. $result = mysqli_fetch_array($q_handle,MYSQLI_ASSOC);
  6. $return_array[$i] = $result;
  7. };
  8. if (!$q_handle) {
  9. error_log("ERROR SQL: $sql\n");
  10. };
  11. $this->result_rows = $return_array;
  12. if (!$q_handle) {
  13. return(1);
  14. } else {
  15. return(0);
  16. };
  17. }
nospor
Zgodnie z komunikatem $q_handle jest FALSE. Czego tu nie rozumiesz?
$q_handle = mysqli_query($this->link_ident, $sql); to zwraca ci FALSE. Masz błąd bazy. Wyswietl go sobie.

ps: przenosze bo z obiektówką to nie ma żadnego zwiazku
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.