0

I have this php script for get the value

<?php
header("Content-type: application/json; charset=utf8_general_ci");
require_once('db.php');
$query = 'SELECT * FROM `hebreux`';
$stm = $db->prepare($query);
$stm->execute();
$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);

When I ran the script I have the value below

[{"francais":"Bonjour","hebreux":"???","chemin":""},{"francais":"Bonne semaine","hebreux":"???? ???","chemin":""}]

My question is why I have ??? and not the hebrew text? in my mysql database I show the hebrew value I have charset utf8_general_ci for the hebrew value.

Thank you if you can help me.

Serg
  • 2,346
  • 3
  • 29
  • 38
  • `charset=utf8_general_ci` is not a thing in the context of HTTP. – CBroe Aug 13 '20 at 10:49
  • what can I do then please thank you –  Aug 13 '20 at 11:37
  • See "question mark" in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored . Note: the question marks were created during `INSERT`, and the Hebrew text is lost. – Rick James Aug 13 '20 at 21:40

1 Answers1

-1

you shoud adjust 2 sides one side in your code by setting connection charset by execute these queries after connection start with your db

header("Content-type:application/json;charset=utf8_general_ci");
require_once('db.php');
$sql = 'SET NAMES "UTF8"';
$stm = $db->prepare($sql);
$stm->execute();
$sql = 'SET CHARACTER SET utf8' ;
$stm = $db->prepare($sql);
$stm->execute()
$sql = "SET COLLATION_CONNECTION = 'utf8_unicode_ci'" ;
$stm = $db->prepare($sql) ;
$stm->execute()
 

$query = 'SELECT * FROM `hebreux`';
$stm = $db->prepare($query);
$stm->execute();
$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);

then your files where you write your code open with any program like Notepad++ that allow you to adjust file Encoding then change all files to UTF-8-BOM

You can adjust database by any way like If you're using the PDO abstraction layer with PHP ≥ 5.3.6, you can specify charset in the DSN:

$dbh = new PDO('mysql:charset=utf8mb4');

If you're using mysqli, you can call set_charset():

$mysqli->set_charset('utf8mb4');       // object oriented style
mysqli_set_charset($link, 'utf8mb4');  // procedural style

If you're stuck with plain mysql but happen to be running PHP ≥ 5.2.3, you can call mysql_set_charset.

If the driver does not provide its own mechanism for setting the connection character set, you may have to issue a query to tell MySQL how your application expects data on the connection to be encoded: SET NAMES 'utf8mb4'.

Akram Elhaddad
  • 194
  • 3
  • 7