Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]Błąd przy wyświetlaniu danych
warland
post
Post #1





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 22.01.2018

Ostrzeżenie: (10%)
X----


Witam.
Proszę o pomoc.
Skrypt pobiera dane z bazy i wyświetla w infowindow.
Działają tylko te które w bazie mają liczby całkowite a te z dziesiętnymi już nie.
Przykład:
gdy w kolumnie 1, 2 i 3 są liczby całkowite (23,34 - 23 - 34) to ok . W chwili gdy w jednak pojawiają się liczby dziesiętne (23.5,34 - 23.5 - 34) pojawia się problem:
Kod
Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 11 - Invalid query:
        SELECT
        SUM(modify_effect.population),
        SUM(modify_effect.culture),
        SUM(modify_effect.gdp),
        SUM(modify_effect.treasury),
        SUM(modify_effect.military),
        SUM(modify_effect.support)
        FROM modify_effect
            LEFT JOIN land_modifier
            ON modify_effect.id = land_modifier.modify_effect_key
        WHERE land_modifier.land_key =;


większy wycinek kodu:
  1. function get_count_of_account_land($account_key)
  2. {
  3. $this->db->select('COUNT(*) as count');
  4. $this->db->from('land');
  5. $this->db->where('account_key', $account_key);
  6. $query = $this->db->get();
  7. $result = $query->result_array();
  8. return isset($result[0]['count']) ? $result[0]['count'] : 0;
  9. }
  10. // Get all modify effects
  11. function get_sum_effects_of_land($land_key)
  12. {
  13. $land_key = mysqli_real_escape_string(get_mysqli(), $land_key);
  14. $query = $this->db->query("
  15. SELECT
  16. SUM(modify_effect.population) as population,
  17. SUM(modify_effect.culture) as culture,
  18. SUM(modify_effect.gdp) as gdp,
  19. SUM(modify_effect.treasury) as treasury,
  20. SUM(modify_effect.military) as military,
  21. SUM(modify_effect.support) as support
  22. FROM modify_effect
  23. LEFT JOIN land_modifier
  24. ON modify_effect.id = land_modifier.modify_effect_key
  25. WHERE land_modifier.land_key = " . $land_key . ";
  26. ");
  27. $result = $query->result_array();
  28. if ( isset($result[0]) ) {
  29. return $result[0];
  30. }
  31. return [];
  32. }
  33. // Get count of modifiers
  34. function get_sum_modifiers_for_land($land_key)
  35. {
  36. $land_key = mysqli_real_escape_string(get_mysqli(), $land_key);
  37. $query = $this->db->query("
  38. SELECT
  39. modify_effect.id,
  40. modify_effect.name,
  41. COUNT(modify_effect.id) AS count
  42. FROM modify_effect
  43. LEFT JOIN land_modifier
  44. ON modify_effect.id = land_modifier.modify_effect_key
  45. WHERE land_modifier.land_key = " . $land_key . "
  46. GROUP BY modify_effect.id;
  47. ");
  48. $result = $query->result_array();
  49. if ( isset($result[0]) ) {
  50. return $result;
  51. }
  52. return [];
  53. }
  54. // Get sum of effect values
  55. function get_sum_effects_for_account($account_key)
  56. {
  57. $account_key = mysqli_real_escape_string(get_mysqli(), $account_key);
  58. $query = $this->db->query("
  59. SELECT
  60. SUM(modify_effect.population) as population,
  61. SUM(modify_effect.culture) as culture,
  62. SUM(modify_effect.gdp) as gdp,
  63. SUM(modify_effect.treasury) as treasury,
  64. SUM(modify_effect.military) as military,
  65. SUM(modify_effect.support) as support,
  66. (
  67. SELECT COUNT(*) as land_count
  68. FROM land
  69. WHERE account_key = " . $account_key . "
  70. ) as land_count
  71. FROM modify_effect
  72. LEFT JOIN land_modifier
  73. ON modify_effect.id = land_modifier.modify_effect_key
  74. WHERE land_modifier.land_key IN
  75. (
  76. SELECT id
  77. FROM land
  78. WHERE account_key = " . $account_key . "
  79. )
  80. ");
  81. $result = $query->result_array();
  82. if ( isset($result[0]) ) {
  83. return $result[0];
  84. }
  85. return [];
  86. }
  87. // Get all modify of land
  88. function get_all_modify_effects()
  89. {
  90. $this->db->select('*');
  91. $this->db->from('modify_effect');
  92. $this->db->order_by('sort_order', 'asc');
  93. $this->db->where('sort_order !=', 0);
  94. $query = $this->db->get();
  95. return $query->result_array();
  96. }

Czy ktoś może mi pomóc jak to rozwiązać? Gdzie szukać by to poprawić?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 12)
SmokAnalog
post
Post #2





Grupa: Zarejestrowani
Postów: 1 707
Pomógł: 266
Dołączył: 3.07.2012
Skąd: Poznań

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


Pokaż jak wywołujesz get_sum_effects_of_land(...).
Go to the top of the page
+Quote Post
warland
post
Post #3





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 22.01.2018

Ostrzeżenie: (10%)
X----


  1. // Get Land Square
  2. $land_square = $this->game_model->get_single_land($world_key, $coord_slug);
  3. $land_square['sum_effects'] = $this->game_model->get_sum_effects_of_land($land_square['id']);
  4. $land_square['sum_modifiers'] = $this->game_model->get_sum_modifiers_for_land($land_square['id']);
  5. $land_square['ambasada_list'] = false;
  6. if ($land_square['kapitol']) {
  7. $land_square['ambasada_list'] = $this->game_model->get_ambasadas_of_land($land_square['id']);
  8. }
  9. $account = false;
  10. if ($land_square['account_key'] != 0) {
  11. $account = $land_square['account'] = $this->user_model->get_account_by_id($land_square['account_key']);
  12. // This shouldn't happen, but it does
  13. // This is a workaround to ensure issue is not noticed
  14. if (!$account) {
  15. // Unclaim land
  16. $this->game_model->update_land_data($land_square['id'], 0, '', '', 1, '#000000');
  17. $land_square = $this->game_model->get_single_land($world_key, $coord_slug);
  18. $land_square['sum_effects'] = $this->game_model->get_sum_effects_of_land($land_square['id']);
  19. $land_square['sum_modifiers'] = $this->game_model->get_sum_modifiers_for_land($land_square['id']);
  20. $this->game_model->update_land_kapitol_status($land_square['id'], $kapitol = 0);
  21. }
  22. else {
  23. $account = $land_square['account'] = $this->get_full_account($account);
  24. }
Go to the top of the page
+Quote Post
SmokAnalog
post
Post #4





Grupa: Zarejestrowani
Postów: 1 707
Pomógł: 266
Dołączył: 3.07.2012
Skąd: Poznań

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


W którymś momencie $land_square['id'] zawiera pusty string, false albo innego czorta.
Go to the top of the page
+Quote Post
warland
post
Post #5





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 22.01.2018

Ostrzeżenie: (10%)
X----


kłopot w tym że nie wiem..
tak to wygląda w rzeczywistości (po kliknięciu w pola z koordynatami pełnymi (liczby całkowite) nie ma kłopotu.. Problem pojawia się gdy klikamy w pol z koordynatami z liczbami dziesiętnymi..
https://cvinto.pl/land/world/7?land=-49.5,-26

Nie wiem czy nie odpowiada za to ta część kodu (może z tabeli tylko zatwierdza wyniki z liczbami całkowitymi. Chociaż na przykładzie powyższym sama lokalizacja pola 49.5,-26 działa bez zarzutu a jedynie nie chce ładować się infowindow:

  1. // For rounding land coords
  2. function round_down(n) {
  3. if (n > 0) {
  4. return Math.ceil(n / land_size) * land_size;
  5. } else if (n < 0) {
  6. return Math.ceil(n / land_size) * land_size;
  7. } else {
  8. return 0;
  9. }
  10. }
  11.  
  12. // Uppercase words
  13. function ucwords(str) {
  14. return (str + '').replace(/^([a-z])|\s+([a-z])/g, function($1) {
  15. return $1.toUpperCase();
  16. });
  17. }
  18.  
  19. // For number formatting
  20. function number_format(nStr) {
  21. if (!nStr) {
  22. return 0;
  23. }
  24. nStr += '';
  25. x = nStr.split('.');
  26. x1 = x[0];
  27. x2 = x.length > 1 ? '.' + x[1] : '';
  28. var rgx = /(\d+)(\d{3})/;
  29. while (rgx.test(x1)) {
  30. x1 = x1.replace(rgx, '$1' + ',' + '$2');
  31. }
  32. return x1 + x2;
  33. }



infowindow :

  1. // Set land window
  2. function set_window(event) {
  3. // Set Parameters
  4. // Not sure why subtracting land_size on lat makes this work, but results in correct behavior
  5. var lat = round_down(event.latLng.lat()) - land_size;
  6. var lng = round_down(event.latLng.lng());
  7. var coord_slug = lat + ',' + lng;


Ten post edytował warland 30.01.2018, 23:54:16
Go to the top of the page
+Quote Post
SmokAnalog
post
Post #6





Grupa: Zarejestrowani
Postów: 1 707
Pomógł: 266
Dołączył: 3.07.2012
Skąd: Poznań

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


Nie za bardzo mam ochotę analizować taki kod spaghetti, ale sprawdź po prostu czy nie robisz czegoś dziwnego po drodze z tą liczbą. Wypisuj sobie dla testu wartości w różnych miejscach kodu i zobacz co jest nie tak.
Go to the top of the page
+Quote Post
warland
post
Post #7





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 22.01.2018

Ostrzeżenie: (10%)
X----


tak też robię ale na razie bez rezultatu. Dziękuję za próbę pomocy.
Go to the top of the page
+Quote Post
SmokAnalog
post
Post #8





Grupa: Zarejestrowani
Postów: 1 707
Pomógł: 266
Dołączył: 3.07.2012
Skąd: Poznań

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


A czy problemem nie jest przypadkiem to, że po prostu operujesz na nieistniejącej krainie?

  1. $land_square = $this->game_model->get_single_land($world_key, $coord_slug);


Co to zwraca, kiedy kraina nie zostaje znaleziona?

Włącz sobie wyświetlanie wszystkich błędów, łącznie z notice. To naprawdę pomaga.
Go to the top of the page
+Quote Post
warland
post
Post #9





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 22.01.2018

Ostrzeżenie: (10%)
X----


Cały problem tkwi w tym kodzie. round_down zaokrągla wszystkie wartości dziesiętne przez co pozycje z kolumny które mają taką wartość dziesiętną są zaokrąglane i pokrywają się z innymi przez co powstaje konflikt.
Przykład:
wynik w kolumnie 55 skrypt odczytuje 55 czyli poprawnie
wynik w kolumnie 55.4 skrypt odczytuje też jako 55 czyli nie poprawnie i wywołuje konflikt.

Jak to teraz zapisać by zaokrąglało do 0.5 (lub jednym miejscu po przecinku)? w // For rounding land coords i wczytywało do // Set land window ?
Proszę o pomoc (próbowałem na wiele sposobów ale mi nie idzie za nic). Dotyczy to liczb dodatnich i ujemnych
Czy na to wszystko może też mieć wpływ kod w // For number formatting iż skrypt nie wyświetla pozycji które są z liczbą dziesiętną?


  1. // For rounding land coords
  2. function round_down(n) {
  3. if (n > 0) {
  4. return Math.ceil(n / land_size) * land_size;
  5. } else if (n < 0) {
  6. return Math.ceil(n / land_size) * land_size;
  7. } else {
  8. return 0;
  9. }
  10. }
  11.  
  12. // Uppercase words
  13. function ucwords(str) {
  14. return (str + '').replace(/^([a-z])|\s+([a-z])/g, function($1) {
  15. return $1.toUpperCase();
  16. });
  17. }
  18.  
  19. // For number formatting
  20. function number_format(nStr) {
  21. if (!nStr) {
  22. return 0;
  23. }
  24. nStr += '';
  25. x = nStr.split('.');
  26. x1 = x[0];
  27. x2 = x.length > 1 ? '.' + x[1] : '';
  28. var rgx = /(\d+)(\d{3})/;
  29. while (rgx.test(x1)) {
  30. x1 = x1.replace(rgx, '$1' + ',' + '$2');
  31. }
  32. return x1 + x2;
  33. }
  34.  
  35.  
  36. // Set land window
  37. function set_window(event) {
  38. // Set Parameters
  39. // Not sure why subtracting land_size on lat makes this work, but results in correct behavior
  40. var lat = round_down(event.latLng.lat()) - land_size;
  41. var lng = round_down(event.latLng.lng());
  42. var coord_slug = lat + ',' + lng;


Ten post edytował warland 31.01.2018, 09:57:19
Go to the top of the page
+Quote Post
nospor
post
Post #10





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Porzadkuj kod.
Przeciez te dwa bloki IF/ELSE
if (n > 0) {
return Math.ceil(n / land_size) * land_size;
} else if (n < 0) {
return Math.ceil(n / land_size) * land_size;
}

Robia dokladnie to samo.

Co do zaokraglania to
https://stackoverflow.com/questions/6137986...-to-nearest-0-5
Go to the top of the page
+Quote Post
warland
post
Post #11





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 22.01.2018

Ostrzeżenie: (10%)
X----


bez rezultatu
możliwe że nie potrafię tego odpowiednio podmienić w kodzie.
Wszelkie próby kończą się tym że set_window się nie wyświetla dla pola z liczbą dziesiętną

lat-szerokość geograficzna
lng-długość geograficzna
gdy lat i lng ma np. 55.0 i 48.0 to set_window wyświetla się dla 55 i 48 bez liczby po przecinku
gdy lat i lng ma np. 55.5 i 48.5 to set_window nie wyświetla się dla tego pola ponieważ te kordy odczytywane są jako 55 i 48 czyli duplikują się z tymi z powyższego pola.

za nic nie wiem czemu nie chce czytać z bazy lat i lng jako wartości dziesiętne.
Pewnie bez kogoś kto nie przerobi mi tego kodu ostatniego co podałem nie poradzę sobie (IMG:style_emoticons/default/sad.gif)
Go to the top of the page
+Quote Post
nospor
post
Post #12





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Ty mi nie pisz, ze bez rezultatu, tylko pokaz kod na jaki zmieniles. Skad mam wiedziec co masz nadal zle.
I generalnei wez sie troche do roboty, opisuj problem dokladniej, sprawdzaj jakie wartosci sa na kazdym etapie, np tu:

var coord_slug = lat + ',' + lng;
Masz zrobic:
var coord_slug = lat + ',' + lng;
alert(coord_slug);

by zobaczyc co dokladnie ci sie generuje
Go to the top of the page
+Quote Post
warland
post
Post #13





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 22.01.2018

Ostrzeżenie: (10%)
X----


Udało się. Dziękuję wszystkim na naprowadzenie mnie na właściwą ścieżkę która rozwiązała problem.
Go to the top of the page
+Quote Post

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.08.2025 - 20:59