Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]Brak polskich znaków w danych pobranych z mysql
gogomania
post
Post #1





Grupa: Zarejestrowani
Postów: 51
Pomógł: 1
Dołączył: 7.02.2013

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


Witam,
zacząłem swoją przygodę z datatables i mam problem... Mianowicie, wypluwa dane z mysql bez polskich znaków, w bazie kodowanie utf8_general_ci, plik server_processing.php kodowanie UTF8 (Bez BOM).
connect.php
  1. mysql_connect($serwer, $login, $haslo) or die($connect_error);
  2. mysql_query("SET NAMES 'utf8'");


head.php
  1. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  2. //...
  3. <script type="text/javascript" charset="utf-8">
  4. $(document).ready(function() {
  5. $('#example').dataTable( {
  6. "sDom": 'C<"clear">lfrtip',
  7. "bProcessing": true,
  8. "bServerSide": true,
  9. "sAjaxSource": "../scripts/server_processing.php"
  10. } );
  11. } );
  12. </script>


plik server_processing.php
  1. $aColumns = array(
  2. 'id'
  3. , 'instytucja'
  4. , 'kodpocztowy'
  5. , 'miasto'
  6. , 'ulica'
  7. , 'wojewodztwo'
  8. ); /* Array of database columns which should be read and sent back to DataTables. Use a space where you want to insert a non-database field (for example a counter or static image) */
  9.  
  10. $ignoreColumns = array(
  11. "id"
  12. );
  13.  
  14. $sIndexColumn = "id"; /* Indexed column (used for fast and accurate table cardinality) */
  15.  
  16. $sTable = "ik_test"; /* DB table to use */
  17.  
  18. /* Database connection information */
  19. $gaSql['user'] = "root";
  20. $gaSql['password'] = "";
  21. $gaSql['db'] = "test";
  22. $gaSql['server'] = "localhost";
  23.  
  24. // include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" ); /* REMOVE THIS LINE (it just includes my SQL connection user/pass) */
  25.  
  26. /* If you just want to use the basic configuration for DataTables with PHP server-side, there is no need to edit below this line */
  27. /* Local functions*/
  28. function fatal_error ( $sErrorMessage = '' ){
  29. header( $_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error' );
  30. die( $sErrorMessage );
  31. }
  32.  
  33. /* MySQL connection */
  34. if ( ! $gaSql['link'] = mysql_connect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) ){
  35. fatal_error( 'Could not open connection to server' );
  36. }
  37. if ( ! mysql_select_db( $gaSql['db'], $gaSql['link'] ) ){
  38. fatal_error( 'Could not select database ' );
  39. }
  40.  
  41. /* Paging */
  42. $sLimit = "";
  43. if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' ){
  44. $sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
  45. intval( $_GET['iDisplayLength'] );
  46. }
  47.  
  48. /* Ordering */
  49. $sOrder = "";
  50. if ( isset( $_GET['iSortCol_0'] ) ){
  51. $sOrder = "ORDER BY ";
  52. for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ ) {
  53. if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" ){
  54. $sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."` ".
  55. ($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
  56. }
  57. }
  58.  
  59. $sOrder = substr_replace( $sOrder, "", -2 );
  60. if ( $sOrder == "ORDER BY" ){
  61. $sOrder = "";
  62. }
  63. }
  64.  
  65. /*
  66. * Filtering
  67. * NOTE this does not match the built-in DataTables filtering which does it
  68. * word by word on any field. It's possible to do here, but concerned about efficiency
  69. * on very large tables, and MySQL's regex functionality is very limited
  70. */
  71. $sWhere = "";
  72. if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
  73. {
  74. $sWhere = "WHERE (";
  75. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  76. {
  77. if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
  78. {
  79. $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
  80. }
  81. }
  82. $sWhere = substr_replace( $sWhere, "", -3 );
  83. $sWhere .= ')';
  84. }
  85.  
  86. /* Individual column filtering */
  87. for ( $i=0 ; $i<count($aColumns) ; $i++ ){
  88. if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' ){
  89. if ( $sWhere == "" ) {
  90. $sWhere = "WHERE ";
  91. } else {
  92. $sWhere .= " AND ";
  93. }
  94. $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
  95. }
  96. }
  97.  
  98.  
  99. /*
  100. * SQL queries
  101. * Get data to display
  102. */
  103. $sQuery = "
  104. SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."`
  105. FROM $sTable
  106. $sWhere
  107. $sOrder
  108. $sLimit
  109. ";
  110. $rResult = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  111.  
  112. /* Data set length after filtering */
  113. $sQuery = "
  114. SELECT FOUND_ROWS()
  115. ";
  116. $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  117. $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
  118. $iFilteredTotal = $aResultFilterTotal[0];
  119.  
  120. /* Total data set length */
  121. $sQuery = "
  122. SELECT COUNT(`".$sIndexColumn."`)
  123. FROM $sTable
  124. ";
  125. $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  126. $aResultTotal = mysql_fetch_array($rResultTotal);
  127. $iTotal = $aResultTotal[0];
  128.  
  129.  
  130. /*
  131. * Output
  132. */
  133. $output = array(
  134. "sEcho" => intval($_GET['sEcho']),
  135. "iTotalRecords" => $iTotal,
  136. "iTotalDisplayRecords" => $iFilteredTotal,
  137. "aaData" => array()
  138. );
  139.  
  140. while ( $aRow = mysql_fetch_array( $rResult ) ) {
  141. $row = array();
  142. for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
  143.  
  144. if ( $aColumns[$i] == "version" ) {
  145. /* Special output formatting for 'version' column */
  146. $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
  147. }else if ( $aColumns[$i] == "zdjecie" ) {
  148. /* Special output formatting for 'miniaturka' lub 'zdjecie' column */
  149. $row[] = "<img style='width: auto; height: 50px;' src='".$aRow[ $aColumns[$i] ]."' />";
  150. } else if ( $aColumns[$i] != ' ' ) {
  151. /* General output */
  152. if ( !in_array($aColumns[$i], $ignoreColumns) ) {
  153. $row[] = $aRow[ $aColumns[$i] ];
  154. }
  155. }
  156. }
  157. $output['aaData'][] = $row;
  158. }
  159. echo json_encode( $output );



Dane w tabeli wyplutej przez json wyglądają tak:
Warmi?sko - Mazurskie
Urz?d Gminy

Dane w tabeli mysql wyglądają prawidłowo, są Polskie znaki.
Ktoś może mi podpowiedzieć o co chodzi? Jak mogę to rozwiązać?

Ten post edytował gogomania 10.10.2013, 09:17:38
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: 7.10.2025 - 02:00