Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]Zapytanie + inner join = wynik jako json
skabszo
post
Post #1





Grupa: Zarejestrowani
Postów: 69
Pomógł: 0
Dołączył: 27.08.2004

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


Cześć wszystkim,

Mam problem z poniższym kodem. Otóż poprawnie łączy się on z bazą danych, ale nie wyświetla niczego - strasznie długo mieli i wyswietla tylko: {"aaData":}. To samo zapytanie wywołane przez phpmyadmin pięknie wyświetla wynik. Tabele nie zawierają dużej ilości danych - tabela wizyty to jakieś 1200 wierszy, pozostałe łącznie jakieś 400 wierszy.

  1. <?php
  2.  
  3. // set up the connection variables
  4. $db_name = '';
  5. $hostname = 'localhost';
  6. $username = '';
  7. $password = '';
  8.  
  9. // connect to the database
  10. $dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);
  11.  
  12. // a query get all the records from the users table
  13. $sql = 'select
  14. WIZYTY.ID as visitno,
  15. WIZYTY.DATA_ZGLOSZ as visitdate,
  16. ANIMALS.NAZWA as animalname,
  17. CUSTOMER.NAZWA as customername,
  18. DOCTORS.NAZWA as doctorname,
  19. WIZYTY.AMOUNT as visittotal
  20. from WIZYTY
  21. inner join ANIMALS on ANIMALS.ANI_ID = WIZYTY.ANI_ID
  22. inner join CUSTOMER on CUSTOMER.CUST_ID = WIZYTY.CUST_ID
  23. inner join DOCTORS on DOCTORS.DOCTOR_ID = WIZYTY.DOCTOR_ID
  24. group by WIZYTY.ID order by WIZYTY.ID';
  25.  
  26. // use prepared statements, even if not strictly required is good practice
  27. $stmt = $dbh->prepare( $sql );
  28.  
  29. // execute the query
  30. $stmt->execute();
  31.  
  32. // fetch the results into an array
  33. $result = $stmt->fetchAll( PDO::FETCH_ASSOC );
  34.  
  35. // convert to json
  36. $json = json_encode( $result );
  37.  
  38. // echo the json string
  39. echo "{\"aaData\":";
  40. echo $json;
  41. echo "}";
  42. ?>


Dlaczego tak jest? Ma ktoś pomysł, jak usprawnić ten kod i sprawić by działał?

Ten post edytował skabszo 21.03.2016, 13:46:58
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: 23.12.2025 - 02:42