I am trying to show Bengali text (more specifically, username) to the user, which is coming from the Database. But I am getting multiple question marks in the view (front-end) (???) instead of the username. I have attached an image here so that you can understand it nicely.
How can I solve this problem?
I will be very grateful if you kindly help me with the solution.
Screenshot of the problem
Is all of your data (i.e., the text you type in your files and the text that your users submit to your database) using UTF-8 or UTF-16 encoding?
You may wish to do that. In HTML, add this right after the head tag:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
You could also specify the language of your HTML document:
The W3 also says this:
When the page contains content in another language, add a language attribute to an element surrounding that content. This allows you to style or process it differently. For example:
<p>The title is "<span lang="fr">Le Bon Usage</span>".
I checked the source code of the web page and I can guarantee that inserting html attributes regarding language won’t fix your problem.
I see you are using html files, how do you fetch from the database using it?
I checked your code and I think the issue is database and encoding related. The PHP code spits out the question marks, so that’s why you see question marks on the page.
This is caused by one (admittedly not very smart) design flaw in how our database connections work.
By default, our PHP installation uses the
latin1 charset for MySQL character encoding, but phpMyAdmin always uses
utf8. This means that if you insert special characters with PHP, it will show up as gibberish in phpMyAdmin. And if you enter special characters with phpMyAdmin, it will show as gibberish in PHP.
The text in your database looks fine when looked at from phpMyAdmin, and your code doesn’t specify a character set, so I think the database query in PHP returns gibberish. And you’re feeding that to
htmlspecialchars, which has no idea on how to encode the gibberish and just prints question marks.
The fix is quite simple: in your database connection snippet, use the
mysqli_set_charset function to set the connection charset to
utf8mb4 (doesn’t really matter which). This should make the text appear correctly on your page.
There is an AddType handler in the .htaccess code that tells the server to execute
.html files as PHP code.
I guessed such thing but I was confused over why make it seem like a html file.
Some people have the idea that having a HTML page with the .html extension is “right” and outputting HTML with a .php file is “wrong”. Or that .html is “nicer”. Or some misguided notion on that .html is better for SEO than .php.
All completely unfounded of course. Personal preference is a thing of course, but the idea that search engines care about file extensions is nonsense. The only legitimate use case I can think of is if you started out with plain HTML and integrated PHP code later on and didn’t want to break existing links. But even then I would just redirect the URLs to the appropriate extension instead of lying about file types.
And if you do prefer .html for web URLs, I would always use rewrite rules to rewrite the .html URL to the .php file on disk. Editors/IDEs may not like or recognize PHP code in HTML files, which makes it harder to develop with. So if you have .html files that are just HTML, you’re not feeding them through the PHP interpreter for no reason.
Thank you for your reply. I have added the mysqli_set_charset function in my database connection snippet, and it worked.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.