0

I'm processing a request through ajax and return a json encoded result.

$sql    = "SELECT * from products WHERE product_name='".$choosen."'";
$result = mysql_query($sql) or die("Error");
$row    = mysql_fetch_assoc($result);

echo json_encode($row);

I parse this json in my javascript as follows,

success:function(result)
        { var Data = JSON.parse(result); }

Every data is returned and ended up successfully except columns contain ± symbol (column_1 and column_2). I found it by remove this(±) symbol manually from database, then it works fine.

So I've decided to replace ' ± ' with ' + or - ' in every instance. I rewrite the code as follows,

$sql="SELECT * from products WHERE product_name='".$choosen."'";
$result = mysql_query($sql) or die("Error");
$row    = mysql_fetch_assoc($result);

$row["column_1"]   = str_replace('±','+ or -',$row["column_1"]);
$row["column_2"]   = str_replace('±','+ or -',$row["column_2"]);

echo json_encode($row);

Now the problem is, **str_replace() isn't working for this character ±.

I have two option to kill this bug. Either replacing '±' this symbol or find a way to encode ± this symbol, to json without any replacement. But I don't know how to find either ways. Please help me out.

JaiSat
  • 2,356
  • 1
  • 14
  • 15
  • 1
    possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – deceze Nov 18 '13 at 12:28
  • Also read [What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text](http://kunststube.net/encoding/) – deceze Nov 18 '13 at 12:28
  • What is the character encoding of the string in your database and the php file? – Paul S. Nov 18 '13 at 12:28
  • in my DB I defined 'DEFAULT CHARSET=latin1$$' Everything is default and I'm using ubuntu's default text editor. – JaiSat Nov 18 '13 at 12:35
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – h2ooooooo Nov 18 '13 at 12:38

3 Answers3

0

Set your MySQL character set when doing the query using special characters.

Edit: As deceze mentioned in the comment, read this answer: UTF-8 all the way through

Community
  • 1
  • 1
Joran Den Houting
  • 3,149
  • 3
  • 21
  • 51
0

You can use chr() method to re-create the UTF-8 character bytes like this:

$row["column_1"]   = str_replace(chr(0xc2).chr(0xB1),'+ or -',$row["column_1"]);
$row["column_2"]   = str_replace(chr(0xc2).chr(0xB1),'+ or -',$row["column_2"]);

That is assuming you have UTF-8 being used correctly in your database and the issue is just the use of the ± character in the actual PHP code.

Also you might want to use the HTML entity instead of the string '+ or -'

±

which will render as ±

StigM
  • 711
  • 6
  • 12
  • sorry buddy, this isn't work out.But I used '±' this. Thanks. I never knew such html entity exists. – JaiSat Nov 19 '13 at 09:09
  • I notice above your mention of 'latin1' which means you could try $row["column_1"] = str_replace(chr(0xB1),'+ or -',$row["column_1"]); which is the 'single byte' character reference of plusmin. Sounds like you found a solution though, the best solution is always the one that works ;) – StigM Nov 19 '13 at 22:07
0

Thanks to everyone, for took your time to answer my question. I've solved this issue in following way,

$row["column_1"] =str_replace('?','±',utf8_decode($row["column_1"]));
$row["column_2"] =str_replace('?','±',utf8_decode($row["column_2"]));

I tried with utf8_decode() function alone. But '±' symbol replaced with '?'. So again I used replacing function to replace unknown symbol. Anyway I cleared it temporarily and still working on clearing this permanently. Thanks for you guys for gave me such a good answers.

JaiSat
  • 2,356
  • 1
  • 14
  • 15