Ishan Garg
Ishan Garg

Reputation: 318

Character encoding error in Php

Hi I am storing some text in a mysql column. The error I am getting is whenever there is an apostrophe (') in the text it's getting displayed as '’'even though apostrophe is being displayed in the database Any ideas what would be the error? I have spent days but no luck

You can check the error here : http://beta.writiely.com/

Here how it's connecting and fetching result

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * from textDocs";
$result = $conn->query($sql);

$res = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
array_push($res,array('t'=>$row['t'],'co'=>$row['cover'],
     'text'=>$row['txt']));
    }
} else {
    return false;
}
$conn->close();

Upvotes: 0

Views: 1795

Answers (1)

Rick James
Rick James

Reputation: 142208

’ is the Mojibake for the utf8 RIGHT SINGLE QUOTATION MARK, which is not normally used in computer circles. (You probably got it from some Word processing package.)

htmlentities should not be relevant to this discussion. If the web page has ’, you have a big mess. I don't think it has ’ based on what you have said.

Yes, $conn->set_charset("utf8"); is needed. And this is usually the fix for this type of problem.

However, you also need the table/column to be CHARACTER SET utf8, is it? Do SHOW CREATE TABLE to check.

utf8 and utf8mb4 do not differ in this context. The latter is needed for Emoji and some of Chinese.

The "collation" (such as utf8_general_ci) is not relevant to this discussion. However, utf8_general_ci applies only to utf8, so some of the comments were unambiguous.

Summary:

  • Your bytes are apparently utf8,
  • Do the set_charset,
  • Check the table/column,
  • HTML4 needs <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">; HTML5 can shorten it: <meta charset="UTF-8"> (but apparently this is not the issue).

Upvotes: 1

Related Questions