-1

I Am trying to insert Arabian words/characters into the database via PHP. I created my database and table with collations.

CREATE DATABASE Arab CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `posts1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) CHARACTER SET utf8 NOT NULL,
  `title_seo` varchar(200) DEFAULT NULL,
  `content` text CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 CHARACTER SET utf8 COLLATE utf8_general_ci;;


insert into posts1(title,title_seo,content)values('حسناً','good','حسناً');

The Insert statements above works fine when I inserted data from phpMyAdmin and I can see the arab character inserted successfully from phpMyAdmin MySQL database

Here is my Problem with PHP

When I tried to Insert Arab Characters with PHP, it does not work rather it just inserts Question marks ???????

Here is my PHP code:

<?php

//header('content-type: text/html; charset=utf-8');

ini_set('default_charset', 'utf-8');
header('Content-type: text/html; charset=utf-8');



$servername = "localhost";
$username = "root";
$password = "";
$dbname = "arab";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
mysqli_set_charset($conn,"utf8");
mysqli_query($conn,"SET NAMES 'utf8'");
mysqli_query($conn,'SET CHARACTER SET utf8'); 

$en = "OK";
$ar = "حسناً";
$sql = "INSERT INTO posts1 (title,title_seo,content)
VALUES ('حسناً','test','حسناً')";

if (mysqli_query($conn, $sql)) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);


?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38
  • 1
    Is your PHP file UTF8 encoded ? – E-telier Dec 03 '21 at 20:23
  • collation use: `utf8mb4_general_ci`, or starting with `MySQL 8+` collation is: `utf8mb4_0900_ai_ci`. `uft8mb4` means that each character is stored as a maximum of 4 bytes in the UTF-8 encoding scheme. – berend Dec 03 '21 at 20:33

1 Answers1

0

See "question mark" (not "black diamond") in Trouble with UTF-8 characters; what I see is not what I stored

Alas, the data is not recoverable.

Do SELECT HEX(title), title FROM posts1 to see what got stored:

D8ADD8B3D986D8A7D98B  -- Correct Arabic
3F3F3F3F3F            -- question marks
C398C2ADC398C2B3C399E280A0C398C2A7C399E280B9  -- "double encoding"
Rick James
  • 135,179
  • 13
  • 127
  • 222