0

I try to save the rows of a table in an array but I am getting an empty array.
My code is:

$a = array();
$b = array();
if ($result->num_rows > 0){
    while ($row = $result->fetch_assoc()) {
        $b["id"] = $row["id"];
        $b["title"] = $row["title"];
        $b["description"] = $row["description"];
        $b["lat"] = $row["lat"];
        $b["lng"] = $row["lng"];
        $b["walkId"] = $row["walkId"];
        echo json_encode($b);
        array_push($a,$b);
    }
    echo json_encode($a);
}

Using echo json_encode($b); I see the results one by one but the array a is empty.

EDIT 1

After Random's help, using the echo print_r($a,true); I am getting the below screenshot: error

And the screenshot from other tables in which I get the data correctly is: correct

EDIT 2

problem with json_encode. I get the below [{"id":"55","title":"\u00ce\u203a\u00ce\u00bf\u00cf\u2026\u00ce\u00bb\u00ce\u00bf\u00cf\u2026\u00ce\u00b4\u00ce\u00ac\u00ce\u00b4\u00ce\u00b9\u00ce\u00ba\u00ce\u00b1","description":"\u00ce\u201c\u00ce\u00b5\u00ce\u00bd\u00ce\u00b9\u00ce\u00ba\u00ce\u00ae \u00ce\u00b1\u00ce\u00bd\u00ce\u00b1\u00cf\u2020\u00ce\u00bf\u00cf\u0081\u00ce\u00ac \u00cf\u0192\u00cf\u201e\u00ce\u00b1 x\u00ce\u00b1\u00cf\u0081\u00ce\u00b1\u00ce\u00ba\u00cf\u201e\u00ce\u00b7\u00cf\u0081\u00ce\u00b9\u00cf\u0192\u00cf\u201e\u00ce\u00b9\u00ce\u00ba\u00ce\u00ac \u00cf\u201e\u00ce\u00b7\u00cf\u201a \u00ce\u00b3\u00ce\u00b1\u00cf\u0192\u00cf\u201e\u00cf\u0081\u00ce\u00bf\u00ce\u00bd\u00ce\u00bf\u00ce\u00bc\u00ce\u00af\u00ce\u00b1\u00cf\u201a \u00cf\u201e\u00ce\u00b7\u00cf","lat":"40.634356","lng":"22.940716","walkId":"92"},{"id":"56","title":"\u00ce\u2018\u00ce\u00b3\u00ce\u00bf\u00cf\u0081\u00ce\u00ac \u00ce\u0153\u00ce\u00bf\u00ce\u00b4\u00ce\u00b9\u00ce\u00ac\u00ce\u00bd\u00ce\u00bf","description":"H \u00ce\u00b9\u00cf\u0192\u00cf\u201e\u00ce\u00bf\u00cf\u0081\u00ce\u00af\u00ce\u00b1 \u00ce\u00bc\u00ce\u00b9\u00ce\u00b1\u00cf\u201a \u00ce\u00b9\u00cf\u0192\u00cf\u201e\u00ce\u00bf\u00cf\u0081\u00ce\u00b9\u00ce\u00ba\u00ce\u00ae\u00cf\u201a \u00ce\u00b1\u00ce\u00b3\u00ce\u00bf\u00cf\u0081\u00ce\u00ac\u00cf\u201a. \u00cf\u201e\u00ce\u00b1 X\u00ce\u00b1\u00cf\u0081\u00ce\u00b1\u00ce\u00ba\u00cf\u201e\u00ce\u00b7\u00cf\u0081\u00ce\u00b9\u00cf\u0192\u00cf\u201e\u00ce\u00b9\u00ce\u00ba\u00ce\u00ac \u00cf\u201e\u00ce\u00b7\u00cf\u201a \u00ce","lat":"40.634609","lng":"22.941081","walkId":"92"}

user4292106
  • 441
  • 1
  • 11
  • 24
  • Are you sure it is getting inside the loop? – Sougata Bose May 21 '15 at 08:07
  • Does `$b` actually contain any data? – dotty May 21 '15 at 08:07
  • 5
    why make it hard for yourself, just use `$a[] = $row;` – Kevin May 21 '15 at 08:08
  • Even if this worked, you'd only get the last value returned by fetch_assoc() because you're overwriting values all the time. Why not just push $row into $a ?? – binderbound May 21 '15 at 08:08
  • sgt BOSE the above is copy paste of my script, dotty the $b contains data (as I said with echo I am getting the data one by one) and Ghost I tried your solution by the same again. – user4292106 May 21 '15 at 08:15
  • 1
    this code works... it may come from somewhere else ? http://sandbox.onlinephpfunctions.com/code/3dd04a1f48a919a9727451efa8fb54162c44eb2f – Random May 21 '15 at 08:18
  • no it doesn't from somewhere else. I have the same code in order to take the data from another table and for that it's work perfect but in this i am getting an empty array. – user4292106 May 21 '15 at 08:24
  • As I said it is copy paste from my script and with echo inside the while loop I am getting the data one by one but the array a is empty. – user4292106 May 21 '15 at 08:39
  • I tried the try-catch but it doesn't show anything. Also I used the ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); but it doesn't show anything too. – user4292106 May 21 '15 at 08:53
  • Can you check the number of rows that you are getting from the db? – Prerak Sola May 21 '15 at 09:22
  • @PrerakSola The problem is that he prints the content of $b, which is correct, so we're sure $a should be filled with it (and not empty)... – Random May 21 '15 at 09:33
  • The code is working fine. I tested it on my side. There must be something else, else where that you are getting wrong. – Prerak Sola May 21 '15 at 10:04
  • Check with the column names for typing mistakes or you can try recreating that table. – Prerak Sola May 21 '15 at 10:15
  • Using the echo print_r($a,true); I am getting the data (finally) but in different version from that which returns the json_encode. In table I have greek characters but I have set the utf 8 and also in another table with greek I am getting the data correctly. – user4292106 May 21 '15 at 16:05
  • First thing I note is the special `?` that appears 3 time, the problem must come from here. Try to edit the 3 descriptions that contain the special `?` so it doesn't appear again in the print_r. Isn't it a greek char that cannot be read ? If it doesn't resolve the problem, please post the text, instead of images, so it is easier for me to manipulate. – Random May 22 '15 at 13:25
  • Yes you are right Random. The problem was the special question mark. Now all data appears. But now the problem is with json_encode. When I am getting the data via http calls in my android app the greek characters are appeared strange. Any suggestion? Thanks for your help and for your time. – user4292106 May 24 '15 at 07:45
  • Cheers ! For your second question, I think you should make a new topic, because it is something really different, and I'm not sure I can help. I'll post the answer of the first question here for people who get the same problem. You should also remove comments that didn't help to resolve it. – Random May 25 '15 at 07:48

1 Answers1

0

First thing I note is the special ? that appears 3 time. The problem must come from here.
Try to edit the 3 descriptions that contain the special ? so it doesn't appear again in the print_r.
That must turn array_push into an error.
You could also try to encode it in latin to make it safe. (please follow @deceze link to understand what to do)

Random
  • 3,158
  • 1
  • 15
  • 25
  • `utf8_encode` converts from Latin-1 to UTF-8. The characters shown in the screenshot are clearly not Latin-1 encodable characters, so `utf8_encode` won't do any good here. – deceze May 25 '15 at 08:19
  • @deceze What should be used in this case so ? ( I'm french, so I always use utf8 to solve problems :) ) – Random May 25 '15 at 08:29
  • http://stackoverflow.com/questions/279170/utf-8-all-the-way-through and [Handling Unicode Front To Back In A Web App](http://kunststube.net/frontback/) – deceze May 25 '15 at 08:30
  • @deceze I don't believe this is a duplicated, since the main question is about array_push problem... There were unexpected characters in the DB... The duplicate is for the 2nd question, which is actually about encoding... – Random May 25 '15 at 08:45
  • There is no actual problem with `array_push` here, it's all just a problem with encodings. – deceze May 25 '15 at 08:47
  • When I posted the question I couldn't imagine that the problem was the encoding. The problem has been solved with @random's help. About the problem with encoding of greek characters I solved it with below code: $data[] = $row; $json = html_entity_decode(json_encode($data, JSON_UNESCAPED_UNICODE)); echo $json; – user4292106 May 26 '15 at 10:16