0

I'm trying to figure out how to get Hebrew characters. In my connection.php I have $con->set_charset('utf8');, so letters appears in input fields and recorded properly in MySql database with this connection.php:

<?php
header("Access-Control-Allow-Origin: *");
header('Content-type: text/xhtml; charset=windows-1255');

$con = new mysqli('localhost:XXXX', 'user', 'password');
$con->set_charset('utf8');

if (!$con)
{
    echo 'Not Connected To Server';
}

if (!mysqli_select_db ($con, 'mydb'))
{
    echo 'Database Not Selected';
}
?>

But in several places, seems like, when I'm trying to get it from list, for example below home.ts this.function(); http request, it appears this way:

enter image description here

 function() {
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    let options = new RequestOptions({ headers: headers });
      let postParams = '&user=' + this.user;
      this.http.post(this.phpPath + "select.php", (postParams), options)
        .subscribe(data => {         
          var nmb = (data.text().split(".").length);
          let x = data.text().split(".");
          for (var i = 0; i < nmb; i++) {
            var c = x[i];          
              this.list.push({ id: i, name: this.s + c }, );
          }
        }, error => {
          console.log(error);
        });     
  }

particularselect.php uses connection.php:

<?php
require 'connection.php';
$user = mysqli_real_escape_string($con, $_POST["user"]);
$sql = "SELECT name FROM tab where user='".$user."'";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo $row["name"].'.';
    }
} else {
    echo "0 results";
}
mysqli_close($con);
?>

I've tried to add <meta charset="UTF-8"> to home.html, but index.html already contains it. And also I've tried to save home.html with notepad encoded with UTF-8, but nothing helps.

header(‘Content-type: text/xhtml; charset=windows-1255’); already in connection.php, and in result I have Hebrew characters in MySql database record from other http requests and .php back-end.

For example input in application:

enter image description here

or in MySql table:

enter image description here

But this does not affect button, as well as several other places in same application UI with same connection.php, like this one from particular code:

enter image description here

if there is no header(‘Content-type: text/xhtml; charset=windows-1255’); it looks this way:

enter image description here

or in application:

enter image description here

but it is different from:

enter image description here

I’m not quite understand, what I'm doing wrong and have to change with XHTML, sending UTF-8 and promising windows-1255, but I’ve tried return it differently, as a json from select.php with array $myArray[] = $row; as let obj: MyObj = JSON.parse(data.text()); in http request.

In result console:

\u05d0\u05d1\u05d2\u05d3

And object is undefined

I found this stackoverflow answer, which says that Old-Style ASCII Property Lists allows only ASCII characters, and I have print description of a string. Maybe it is a reason, but I’m not sure, how to do it in Ionic2 TypeScript

EDIT 1:

I've edited connection.php with $con->set_charset('utf8mb4'); instead of utf8, and also I've used $con->query("SET collation_connection = utf8mb4_unicode_520_ci");:

$con = new mysqli('host', 'user', 'password');
$con->set_charset('utf8mb4');
$con->query("SET collation_connection = utf8mb4_unicode_520_ci");

MySql database row on server is utf8_general_mysql500_ci, which contains hebrew characters record and retrieves by request in other places of application.

I'm not sure, how to set <meta charset=UTF-8> for Ionic2 html page, it is already in <head> of project index.html. With adding to home.html or any other page <ion-header>, nothing changes.

in case of \u05d0\u05d1\u05d2\u05d3 now with echo json_encode($myArray, JSON_UNESCAPED_UNICODE); seems like returns proper string with English json names, but hebrew still in Mojibake. Anyway, to avoid Unicode codepoints, in my case it is enough to get echo as $row["name"].'.'; without array and object in http request. But I'm not sure, what to do with Mojibake

EDIT 2:

Show create table, name column contains hebrew:

CREATE TABLE `band` (
 `ID` int(20) NOT NULL AUTO_INCREMENT,
 `name` varchar(300) COLLATE utf8_general_mysql500_ci NOT NULL,
 `style` varchar(100) COLLATE utf8_general_mysql500_ci NOT NULL,
 `member` text COLLATE utf8_general_mysql500_ci NOT NULL,
 `zone` varchar(300) COLLATE utf8_general_mysql500_ci NOT NULL,
 `region` varchar(100) COLLATE utf8_general_mysql500_ci NOT NULL,
 `website` varchar(800) COLLATE utf8_general_mysql500_ci NOT NULL,
 `image` varchar(2083) COLLATE utf8_general_mysql500_ci NOT NULL,
 `user` varchar(700) COLLATE utf8_general_mysql500_ci NOT NULL,
 `num` varchar(200) COLLATE utf8_general_mysql500_ci NOT NULL,
 `registration` varchar(300) COLLATE utf8_general_mysql500_ci NOT NULL,
 PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_general_mysql500_ci

EDIT 3:

<?php
require 'connection.php';   
$user = mysqli_real_escape_string($con, $_POST["user"]);  
$sql = "SELECT name, HEX(name) AS hex_name FROM tab where user='".$user."'";  
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo $row["name"], ' ', $row["hex_name"], '.';
    }
} else {
    echo "0 results";
}    
mysqli_close($con);
?>

result:

enter image description here

EDIT 4:

result with text:

enter image description here

in text:

 ׳׳‘׳’׳“

׳׳•׳¨ ׳¢׳§׳™׳‘׳

׳׳׳•׳ ׳™ ׳™׳¦׳—׳§

copied from console:

enter image description here

in database:

enter image description here

as hebrew text:

אבגד
אור עקיבא
אלוני יצחק

at the same time with another request in other page to same table and record, console:

enter image description here

in app:

enter image description here

  • `$sql = "SELECT name, HEX(name) AS hex_name FROM band where user='$user'";` then `echo $row["name"], ' ', $row["hex_name"], '.';` – Rick James Aug 18 '18 at 22:26
  • @Rick James question edited, but Mojibake is still there with hex_name –  Aug 18 '18 at 22:54
  • I don't like it, but the `\u....` encoding may be the only solution. – Rick James Aug 19 '18 at 01:57
  • @Rick James yes it changes method, but works. I have to mark your answer, because it contains all to make it work –  Aug 19 '18 at 13:04

1 Answers1

0

I see at least 3 problems.

\u05d0\u05d1\u05d2\u05d3 -- is using the Unicode codepoints. Don't do that. Use json_encode(str, JSON_UNESCAPED_UNICODE) to avoid it.

One image shows "Mojibake"; the other shows "black diamonds". Both are discussed here. set_charset('utf8') is only one of about 4 things that need to be consistent.

Re Edit 3: D790 D791 D792 D793 is the correct hex for utf8 encoding for אבגד. So, the data is stored correctly. But the data is not displayed correct (Mojibake). Now, going through the link:

  • The bytes to be stored need to be UTF-8-encoded. -- This is OK.
  • The connection when INSERTing and SELECTing text needs to specify utf8 or utf8mb4. -- Provide SHOW VARIABLES LIKE 'char%'
  • The column needs to be declared CHARACTER SET utf8 (or utf8mb4). -- This is OK.
  • HTML should start with <meta charset=UTF-8>. -- If your are displaying on a web page, see if it is set that way, either in the HTML or by configuring the browser.

But... I am not satisfied that we are yet on the same page. Mojibake should show ×בגד. Can you copy and paste the output? I can't do anything with that image.

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • Hello, thanks for feedback, I've edited my question, following guide by your answer, please check, I'm not sure what is missed there –  Aug 18 '18 at 12:58
  • @ViktorArv - Please provide `SHOW CREATE TABLE` for a table in question. And `SELECT col, HEX(col) ...` for something that is Mojibake. (See my link for more discussion.) The _collation_ (unicode_520 vs general_mysql500) should not matter for Hebrew, except for sorting -- both are character set utf8 or utf8mb4, hence able to encode Hebrew characters. – Rick James Aug 18 '18 at 21:33
  • please check, added as edit 4 with image, text and database record. I have tried both `$con->set_charset('utf8mb4'); ` and `$con->set_charset('utf8'); ` as it shown above in edit 1, `` in `Ionic2`, and other requests and pages shows hebrew characters –  Aug 18 '18 at 23:51
  • @ViktorArv - Some pages work, some do not? View > Source and see if the headers are different. (Meanwhile, I have not figured out how to get from `אבגד` to ` ׳׳‘׳’׳“`. The Gareshes worry me, but I don't know where they are coming from.) – Rick James Aug 19 '18 at 00:05
  • I've added console from other page with php and request, where hebrew characters appears successfully, attempt to use the same described in question before edits.It is same table, same record, request and php from working example was checked, but gives same result. Different pages, but same in html. should I call for ghostbusters... –  Aug 19 '18 at 01:12
  • @ViktorArv - This problem has slimed us. – Rick James Aug 19 '18 at 01:55