0 favourites
  • 10 posts
  • Hi !

    I'm trying to read highscore from an SQL database, I need 10 results containing each 3 datas (ID, PSEUDO, SCORE).

    I found this on JSON on the forum, but it's only return one line :

         $json_data = array();

              while ($data = $req->fetch())


                   $json_data = array(array(array($data['id'])), array(array($data['pseudo'])), array(array($data['score'])));


         $json_data = json_encode($json_data);

         echo '{"c2array":true,"size":[3,1,1],"data":';

         echo $json_data;

         echo '}';

    And here is my response :


    How to have multiple lines ? Can't find clear stuff about JSON ...

    Thanks !

  • Here is the one I am using in my game, I modified this a bit for your use. Also check out my tutorial,which uses a similiar method for posting to MySQL.




    // Check connection

    if (mysqli_connect_errno())


    echo "Failed to connect to MySQL: " . mysqli_connect_error();


    $result = mysqli_query($con,"SELECT * FROM test");

    $json_data = array();

    echo '{"c2array":true,"size":[10,3,1],"data":';

    while($row = mysqli_fetch_array($result))


        $json_data = array(array(array($row['id'])), array(array($row['pseudo'])), array(array($row['score'])));

    $json_data = json_encode($json_data);

    echo $json_data;

    echo ",";

    //To separate two rows


       echo '}';



  • trollface

    Thanks for your help, now I'm getting data, but I can't load them via JSON into an array :


    Is this json wrong ?

  • There's a trailing coma... the JSON parser might not like it.

  • Even without the last comma I cant load from JSON into an array. Here's is what my php page returns :



    ["6"]],[["Joski"]],[["3512"]]], [[

    ["4"]],[["Joski"]],[["3392"]]], [[

    ["5"]],[["Joski"]],[["2538"]]], [[

    ["3"]],[["Joski"]],[["1908"]]], [[

    ["1"]],[["Joski"]],[["3227"]]], [[



    After Load Json into Array, I test if the value at array(0,0) is not egal to 0, if it's not the data are loaded succesfuly else there is an error. The value at 0,0 return 0...

  • Ah ok your format is wrong

    it should be:


    because data should be an is an Array, of 10 Array of 3 array of length 1

    if the dimension where (10,1,1) it would be an array of 10 array of 1 array of length 1


    and if the dimension where something like (10,3,2), it would be an array of 10 array of 3 array of length 2

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Yann, thanks a lot for this clarification on JSON array.

    Even when I use your example on for my php page return, the data are not loaded into the array. If I print MyArray.AsJson I've :

    PHP return :


    [     [






        [["2"],["Joski"],["1333"]] (with or without comma ",")



    Array as json return :

    {"c2array":true, "size":[0,0,0],"data":[]}

    => Empty array so.

    What's wrong with this, using Azure plugin and Json without problem, and now I'm totaly lost.

  • If I load the string I provided directly in C2, it works, though you have to escape " not with \" but with ""

    However since you provide it via php, character escaping shouldn't be too much of an issue (at least on c2 side)

    So what I wonder is, how do you get the data? You're using the AJAX plugin right?

  • Yep I'm using the AJAX plugin and the action AJAX.LastData.

    And even with escaping string with "" : I've got an error.

    I'm trying to output

    ID - Pseudo - Score

    10 lines, 3 columns.

    I think I ll try with token.

  • show the capx and the php file

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)