1
I treat some functions via PHP and in the end return to the client via AJAX useful information. Such information is contained in a array which is converted to JSON through json_encode()
. The problem is that it is sent me an error message with the following:
Syntaxerror: Unexpected end of input
This mistake happens because the json_encode()
is not returning me any array. I have my variable:
$data = array(
'command' => $command,
'message' => $message
);
And the result of var_dump
to the json_encode()
is the following (I put the HTML code for better visualization, run it):
<pre class='xdebug-var-dump' dir='ltr'><small>boolean</small> <font color='#75507b'>false</font>
</pre>
The problem only happens when the variable value $message
is coming from an error in the database, if I write anything in it, the return is normal:
<pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'{"command":2,"message":"Qualquer coisa"}'</font> <i>(length=40)</i>
</pre>{"command":2,"message":"Qualquer coisa"}
Follows below the var_dump
of an example of variable return $message
:
<pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'Erreur de syntaxe pr�s de ''NomeDeUsuario', '$2a$05$Zx3hjrLOnWvNzZpRIhdcPecjreTjjaBkaYLrH7IRcfmn110et/92G',' � la ligne 1'</font> <i>(length=121)</i>
</pre>
Note that this is an error in the database. The error itself is not relevant, I caused it myself to test the return of errors to the customer.
Perfect! I would never think the problem had anything to do with it. Is there any way to "convert" the entire bank to UTF-8, so I don’t have to treat the answers that way? I tried to use
ALTER DATABASE \
db` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci` but the error continues with the wrong accents.– Rafael Almeida
@Rafaelalmeida But are you sure it is the bank that is not in utf-8, or is the connection? This code
ALTER DATABASE
is just a situation, as I said in the link I posted, there are several things to check. For example I useutf8_unicode_ci
, but other users useutf8_general_ci
– Guilherme Nascimento
I just recreated the table: http://i.imgur.com/zm8oUPk.png. But the accents are still wrong. What do you mean by "connection"? I added a header UTF-8 in the PHP class that returns the data and still nothing.
– Rafael Almeida
@Rafaelalmeida you used
$mysqli->set_charset('utf8')
or with PDO:$conn->exec('SET CHARACTER SET utf8');
? Like I said, buddy, it’s not just doing one thing or another, you have to project following every step.– Guilherme Nascimento
Lack of attention, I didn’t see the Dit you made. Thank you very much, I was able to convert everything. :)
– Rafael Almeida
@Rafaelalmeida Success for you!
– Guilherme Nascimento