I generate json trough php, when I get the data in json, I get it in a strange way, sometimes with 2 question marks, like in example:
{"markers":[
{"latitude":"48.6916858",
"longitude":"13.9314643",
"content":"Ort Kn\u00f6belsteiner??<\/b>28.12.2014. - 12:00"}}]}
Here is the code that generates the json:
while ($row = mysqli_fetch_array($fetch, MYSQLI_ASSOC)) {
$row_array['latitude'] = $row['latitude'];
$row_array['longitude'] = $row['longitude'];
$row_array['content'] = utf8_encode($row['grcnt']);
array_push($return_arr,$row_array);
}
echo '{"markers":'.json_encode($return_arr).'}';
I am not sure why am I getting this ?? signs. I tried to google it but no answer. I used json with sql queries combined with html tags, since I need to show this info on a mark (point) on the map. Sql result after query is shown normal, without this question marks. Any help would be appreciated since I am not really sure why is this and in which case it happens. Thank you
SOLVED IT! :)
the problem was in utf8_encode() line, I removed it and made a new query after the connection to the db, wrote this:
mysqli_query ( $link ,"SET NAMES 'utf8'" );
You use json_encode? Php and javascript have diferent ways to see objects. Look at the second parameter of json_encode
Related
I am creating an array of pages using the following query:
$pages = Page::orderBy('sorting')->get()->toArray();
When I then json_encode the output, the output is corrupted when one of the page titles has a quote in it. How can I prevent this?
If you want to return json, you should use:
$pages = Page::orderBy('sorting')->get()->toArray();
return response()->json(['pages' => $pages]);
it should not make any problems. In case you have any, please show what they are.
I've experienced problems with json only when I have DB connection non-UTF and have their some characters. As json_encode needs data be in UTF-8 it might cause problems
Laravel is indeed returning a correct JSON. It seemed the javascript needed to call the object in a weird style, where the object needed to be placed within single quotes: UINestable.init('{ $json) !!}'). So when a single quote was within the JSON it would fault. Thanx though
I try to get name and adress and other data from database which the name and adress atributes are in text format, while the other atributes are in varchar format. I don't understand why some of those name and adress can not be caught while they are there in the db. From about 5000 records there are about 300 records which the name and adress can not be caught in my program. What I got after research is several of the names and addresses started with white space. So after I removed those white spaces, it works just fine. But for the others I just don't understand. They seem just normal text. Here the codes:
$hasil_query1= json_encode($data->getData($query));
$obj1 = json_decode($hasil_query1,true);
$name=$obj1[0]['name'];
$address=$obj1[0]['address'];
When I print $name or $adress, some of them don't appear (not caught). But when I use this code, instead of the above, it works just fine
$result = mssql_query($query);
while($row = mssql_fetch_array($result))
{
$name=$row['name'];
$address=$row['address'];
}
Please help me understand this because I just can not see it untill now.
Thanks for your advance.
Try this
$hasil_query_baru = array();
$hasil_query1= $data->getData($query);
foreach($hasil_query1 as $row){
$hasil_query_baru[] = array_value($row);
}
$hasil_query1 = json_encode($hasil_query_baru);
...
A var_dump may help understand the problem.
var_dump($data->getData($query));
I found the answer from php json_encode() show's null instead of text
"json_encode expects strings in the data to be encoded as UTF-8.
Convert them to UTF-8 if they aren't already"
~phihag
Thanks to Surgeon of Death who gave me that link.
I'm trying to get usernames from this website and this is what I've done:
$div = $html->find('div[class=micro-home-recent-review review-item]');
for ($i=0; $i<count($div); $i++){
$username = $div[$i]->find('div[class=tooltip-fullname]', 0)->find('b', 0)->plaintext;
// I've tried using iconv but apparently it doesn't work
$username = iconv(mb_detect_encoding($username), "UTF-8", $username);
$query = "INSERT INTO users ('name') VALUES ($username)";
$pdo->query($query);
}
Then the newly inserted records in my database are:
As you can see, most of the names are recorded with HTML symbols, which can be displayed normally on browsers, but get messed up when shown as JSON. The same problem happens when I tried to get reviews, and below is the sample JSON of a review:
I need the JSON to show data in my Android app, therefore this problem needs to be solved or the data won't be displayed properly. What could be a possible solution for this? I really need your help and suggestions.
try to use html_entity_decode() function.
use htmlentities_decode() that will solve your problem.
I've read every post about the topic but I don't think I've found a reply to my question, that's driving me crazy.
I got a couple of php files, one stores data into mySQL db, another one read those data: I get data from all over the world and it seems that I succeed to store asiatic character in a right way, but when I try to read those data I can't get those characters back.
As many other users I got ?? instead of the correct chars.
Top of my PHP files I got:
header('Content-Type: application/json; charset=utf-8');
then
mysql_query("SET CHARACTER SET utf8", $link);
mysql_query("SET NAMES 'utf8'", $link);
then
$fab[] = array_map(utf8_encode,$array);
Here if I print_r ($fab) I lost asiatic chars :-(
Then when I do:
$json_string = json_encode($fab); //originale
What I get is "??".
How is the correct way to get the right chars back? The json string is then passed
to an iPhone client.
Any suggestion or help would be sooo appreciated.
Thank you anyway,
Fabrizio
Seems like you're double encoding it? If you get the data from mysql which is already utf8 encoded, what's the point of $fab[] = array_map(utf8_encode,$array); then?
Just had similar thing 2 days ago, when I was accepting utf8 data from an ExtJs form and it was messed up. It was cause I used utf8_encode on the data I received from the script (which was in utf8). So i broke it by double encoding. Maybe same in your case
The problem was what Tseng said: double encoding on the array: I thought I made the right test but simply I didn't.
So the only code I need is:
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
$json_string = json_encode($arr);
echo ($json_string);
Again Tseng, thanx.
I'm having this problem that's driving me nuts.
I've got a MySQL database in which there is a table that contains a text field. I am querying the table in PHP and trying to put the content of the text field for each row into a var.
I am doing something like this:
for ($i=0;$i<$nbrows;$i++){
$id = $data[$i]['ID'];
$description = $data[$i]['DESCRIPTION'];
$mystring .= '<div>'.$id.': '.$description.'</div>';
}
DESCRIPTION is my text field.
I'll pass on the details. The $data array is built from mysql_fetch_array($result). I also tried using objects instead, as I use mysql_fetch_object for all my other routines, but there is no change.
Anyway, the problem is this: if I do "echo $description;" then it works. I am getting my text field data as expected. The problem is that I don't want to output it directly, but add it to a concatenated string, and that is not working. What happens in that case is it seems to be taking $description for some kind of array or object. To make the above example work, I have the replace the string with:
$mystring .= '<div>'.$id.': '.$description[0].'</div>';
So in the concatenated string code, if I treat $description as an array, it works, but obviously I am getting only one letter. (it doesn't actually seem to be an array because I can't implode it).
I tried a million things but I just can't make this work unless I use echo, but that is not what I am trying to do.
There is no issue with fields that aren't text.
Thanks for any ideas!
There is nothing visually wrong with the code you pasted, maybe if you could also add the fetching function as well, we might be able to help you further.
Maybe you could post a var_dump of your $data array?
Have you tried $mystring .= "<div> $id : $description </div>";
Ack, well, you know, hours spent on this and then it becomes obvious after I decide to post for help. This is just because of text encoding/escaping and nothing else. I just didn't see well enough where the problem was actually happening.
Thanks for taking the time to read and respond!