Mobile app version of vmapp.org
Login or Join
Bryan171

: Printing Single Quotes from Database to Screen I have the following string as a varchar field in a MySQL Database: I'm When I perform a query to extract the string and then print it

@Bryan171

Posted in: #Mysql #Php

I have the following string as a varchar field in a MySQL Database:


I'm


When I perform a query to extract the string and then print it to the screen using the PHP print command, I get:


I�m


What could be causing this? I've tried commands like str_replace, htmlspecialchars, and addslashes to no avail. How should I store/print this string?

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Bryan171

3 Comments

Sorted by latest first Latest Oldest Best

 

@Kaufman445

Is the string I'm in your question actually copy-n-pasted from the database via adminer/phpmyadmin/mysqlcli/whatever? Or is it retyped?

I suspect that what's stored in your database is not I'm at all, but rather something like I’m with a typographer's apostrophe. Like the other comments have said, you can and should fix the problem by consistently using a single character set across your CMS, database, database server, and webapp. UTF-8 is a good choice.

Also, as mentioned by jobjorn, you can convert strings' character sets between different character sets. This is especially useful when you don't have control over all components (cms, db, server, webapp, etc).

10% popularity Vote Up Vote Down


 

@Jessie594

Probably your character encoding has not been specified correctly. You can experiment with the effects of character encoding here.

In the code of the PHP file, between the <head> and </head> tags, try using different character encodings such as:


<meta charset='utf-8'>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />


Some questions:


Do any of these suggestions fix the problem?
How is the ' rendered in the source of the webpage (Right Click > View Source)

is it rendered as � or as '?

What editor are you using to save/edit your php files?

10% popularity Vote Up Vote Down


 

@Hamaas447

As Adjam said, it is probably related to Character encoding, a hassle for everyone involved. Check what you are using in your database (probably ISO-8859-1 or UTF-8) and use the same (preferably UTF-8) in your document - both with the meta tag, and the actual file. Depending on what editor you use, you may have an option to save your file in a selectable character encoding.

For a quick fix, you could try the php functions utf8_decode() or utf8_encode().

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme