0

I have the following code:

$rows=array();
while($row=mysql_fetch_assoc($result))
{
    echo $row["titel"];                           
    $rows[] = array_map('utf8_encode', $row);
}
echo json_encode($rows);

Somehow the output is empty, although I take use of the "json_encode" function. The collation of my columns distinguishes between utf8_bin and utf8_general_ci. "echo $row["titel"]" outputs the title of every entry my table contains, so I guess the error has to occur in the encode.

I would appreciate every answer :)

Force0234
  • 243
  • 3
  • 16

4 Answers4

4

your can use this idea :

$rowUtf8encode[]=array_map('utf8_encode',$row);
$rows[]=array_map('json_encode',$rowUtf8encode);
echo($rows);

it work funny

Mohamed Tlili
  • 343
  • 1
  • 11
0

It seems that you'ree missing php package : "php-xml". Make sure that you have "php-xml" ( or "php5-xml" ) package on your server.

you can check by running folloing command on your server :

rpm -qa | grep 'php-xml'

or

rpm -qa | grep 'php5-xml'
turutosiya
  • 1,051
  • 10
  • 17
0

Let the datebase do the encoding!

First you should know a few things:

  • MySQL's latin1 is not ISO 8859-1 but CP1252! See the difference here.
  • utf8_encode encodes only from ISO 8859-1 to UTF-8
  • To convert encodings you should better use mb_convert_encoding (or iconv)
  • mysql_ functions are deprecated! Use PDO or mysqli instead! (look for prepared statements)
  • json_encode generates error messages you can get by calling json_last_error_msg

So let's say your Application works with latin1 encoded strings. Then you should just change the character set for the mysql connection when needed:

//Change to utf8
mysql_query("SET NAMES 'utf8'");
$result = mysql_query($sqlQuery);
$rows = array();
while($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
}
echo json_encode($rows);
//Back to latin1 (if needed)
mysql_query("SET NAMES 'latin1'");
BreyndotEchse
  • 2,192
  • 14
  • 20
  • The code didn't change anything...the echo still doesn't output anything. I've inserted the mysql_query("SET NAMES 'utf8'"); above my query. – Force0234 Nov 17 '14 at 09:23
  • Did you print out the results of `json_last_error_msg`? Did you enable display_errors? -> http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/6575502#6575502 – BreyndotEchse Nov 17 '14 at 09:37
  • yeah, I have entered the error_reporting(-1); and ini_set('display_errors', 'On'); at the beginning of the file. It only says that I should use mysqli in the future. – Force0234 Nov 17 '14 at 10:33
  • And what about `json_last_error_msg` **after** `json_encode`? – BreyndotEchse Nov 17 '14 at 14:02
  • Oh sorry, it says "Malformed UTF-8 characters, possibly incorrectly encoded" – Force0234 Nov 17 '14 at 17:27
  • use [mysql_set_charset()](http://php.net/manual/en/function.mysql-set-charset.php) **Note:** This is the preferred way to change the charset. Using mysql_query() to set it (such as SET NAMES utf8) is not recommended. – vladkras Nov 18 '14 at 03:52
0

Once you have done your connection change the charset. This is due your Database Charset Configuration.

$mysqli = new mysqli("localhost", "UsrChingon", "PwdChingon", "DbChingona"); $mysqli->set_charset("utf8");