JSP64
JSP64

Reputation: 1462

Store special character in mysql database that can be read by JavaScript and HTML

I'm storing data in a MySQL database that may have some special characters. I'm wondering how to store it so that these characters are preserved if they're either output to HTML via PHP OR via JavaScript, e.g. createTextNode.

For example, the division symbol (÷) has the html code ÷, and when I store it as that it shows up fine when put directly into HTML by PHP, but when I pull it into JavaScript using $.getJSON and then insert it with createTextNode it shows up looking like ÷.

I also tried storing the symbol in the SQL directly, but my understanding is that the column would need to be changed from VARCHAR to NVARCHAR and that would cause a performance hit that doesn't seem necessary.

Given that I can modify the SQL, the PHP, or the JavaScript, is there an easy fix here? Maybe a way to unescape the HTML entity in JavaScript?

Upvotes: 1

Views: 1978

Answers (2)

Jack Shedd
Jack Shedd

Reputation: 3531

So there's probably two things going on:

  1. JSON escapes special characters.
  2. Somewhere, something in your code flow is URL encoding the strings too.

So you just need to decode the string in your JavaScript, or you need to find what part of your code is URL encoding those strings and fix it.

Upvotes: 0

StackExchange User
StackExchange User

Reputation: 1220

As answered by Yogesh, you should switch your collation of the DB to utf8_general_ci

Upvotes: 1

Related Questions