-1

I have a php script and a table in a database. The php script selects all the rows and takes them to an array. I have also an Android device which calls this script. Everything workks fine, but if there are some special characters(like "Ñ") in the database row, the script return a NULL value. How can I solve the problem? Here is the code:

 $response = array();

require_once __DIR__ . '/db_connect.php';

$db = new DB_CONNECT();

$result = mysql_query("SELECT *FROM candi") or die(mysql_error());


if (mysql_num_rows($result) > 0) {

$response["candi"] = array();

while ($row = mysql_fetch_array($result)) {

$candi = array();

$candi["id"] = $row["id"];
$candi["name"] = $row["name"];



array_push($response["candi"], $candi);
}
$response["success"] = 1;
BlitZ
  • 12,038
  • 3
  • 49
  • 68
Katherine99
  • 980
  • 4
  • 21
  • 40
  • 6
    [`MySQL`](http://php.net/manual/en/book.mysql.php) (`mysql_*` functions) extension is [***deprecated***](http://php.net/manual/en/function.mysql-connect.php). I suggest to use [`MySQLi`](http://php.net/manual/en/book.mysqli.php) (`mysqli_*` functions) or [`PDO`](http://php.net/manual/en/book.pdo.php) instead. – BlitZ May 22 '13 at 08:03
  • How are you returning the data, are you using json_encode? How is your data encoded in DB? – bouscher May 22 '13 at 08:10

1 Answers1

1

Going out on a limb and assuming you're json_encodeing the value: json_encode expects UTF-8 encoded data. If the data is not UTF-8 encoded, it returns null. Make sure your data is UTF-8 encoded. See UTF-8 all the way through and Handling Unicode Front To Back In A Web App for all the info you need.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
  • Yes deceze, I use json_encode and changing the encoding in the connection to utf-8 I managed to show the value but with a ? instead of Ñ. what should I add to the json_encode? – Katherine99 May 22 '13 at 09:54
  • Then you have messed up your encoding handling somewhere and the data is likely stored incorrectly in the database to begin with. Read the abovelinked articles again, also [What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text](http://kunststube.net/encoding/) if you need even more in-depth background information. – deceze May 22 '13 at 11:01