I have a website (link) where user stories are shown on screen taken from a MySql database. My issue is that apostrophes are not always shown in the end presented data. I use htmlentities($row['MessageText'],ENT_COMPAT|ENT_IGNORE, "UTF-8")
and then echo json_encode($data)
.
The below example comes out without the apostrophes (So it's is its and I'm is Im):
Sounds crazy, but it’s everything; home, work, what people think of me. If I sit still its worse, that’s why I’m always busy
var_dump:
array(5) { ["ID"]=> int(65) ["UserIDLikeChk"]=> string(1) "0" ["MessageText"]=> string(1263) "Sounds crazy, but it�s everything; home, work, what people think of me. If I sit still its worse, that�s why I�m always busy, always moving. Its things in my head that are out of my control. I can but quite irrational, like the worst thing is going to happen and it gets worse when I�m tired. When I�m exhausted I get irrational. When I was young I got panic attacks. I can control them now, but when I came back from Orlando recently, I was very tired and I could feel I was spiralling down a bit, like a panic attack was coming. When that happens all I can see is what is in my head and no one can talk sense to me. I can have a period of a couple of weeks, where I can feel the anxiety underlying, a knot in my stomach and I know if I�m not careful I could have a panic attack. I�m not a very confident person, so that makes the worry worse I think. I try to rationalise things and that makes things better. I use the Calm app, I speak to my mum, I remove myself from people and that calms me down. I also go to the gym to get the kick from the endorphins. I also avoid alcohol now because that makes things much worse. I think I was drinking after the Orlando trip, when I was really tired, I had a heavy night when I went out, and that made it so much worse." ["CntLikes"]=> int(0) ["Type"]=> int(0) }
Code:
$userid = session_id();
$stmt = $con->prepare(
'SELECT
a.ID, ? as UserID, IFNULL(b.UserID,0) as UserIDLikeChk, a.MessageText, a.CntLikes, IFNULL(b.Type,0) as Type
FROM
(
SELECT m.ID, m.MessageText,count(l.ID) as CntLikes
FROM MessageMain m
LEFT OUTER JOIN Likes l ON m.ID = l.PostID
WHERE Valid = 1
GROUP BY m.ID, m.MessageText ORDER BY RAND() LIMIT 10
)a
LEFT OUTER JOIN
(
SELECT PostID, UserID, COUNT(*) AS Type
FROM Likes
WHERE UserID = ?
GROUP BY PostID, UserID
)b
ON a.ID = b.PostID'
);
$stmt->bind_param('ss', $userid, $userid);
$stmt->execute();
$result = $stmt->get_result();
$data = array();
if(mysqli_num_rows($result) > 0) {
While($row = $result->fetch_assoc()) {
$data[] = array (
'ID' => $row['ID'],
'UserID' => $row['UserID'],
'UserIDLikeChk' => $row['UserIDLikeChk'],
'MessageText' => nl2br(htmlentities($row['MessageText'],ENT_COMPAT|ENT_IGNORE, "UTF-8") ),
'CntLikes' => $row['CntLikes'],
'Type' => $row['Type']
);
}
}
echo json_encode($data);