1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

mySQL output as JSON not gathering all rows

Discussion in 'MySQL' started by Stealthrtt, Jan 8, 2022.

  1. #1
    Hey all the example below is what I currently get when I run the query below:
    
    {
      "data": [{
                    "id": "546789434",
                    "photo": "rru7ehklu.jpg"
                 }, {
                    "id": "546789434",
                    "photo": "934bu9rg9w3rg.jpg"
                 },
                  ..........
                 {
                    "id": "546789434",
                    "photo": "er6u57kerethu]}
    
    Code (markup):
    Using this query here:
    SELECT
        CONCAT("{""data"": [",
             GROUP_CONCAT(
                  CONCAT("{""id"":""",fbid,""","),
                  CONCAT("""photo"":""",photoName,"""}")
             )
        ,"]}")
    AS json FROM fbphotos;
    Code (SQL):
    This does produce the needed schema but the two issues are:
    1. There are 440 rows in the fbphotos table and yet this JSON output only has 13...
    2. It truncates abruptly at the 13 row data while it is creating the output.
    If I do a SELECT fbid, photoName FROM fbphotos I get back all 440 rows without an issue.

    [​IMG]

    I checked the 13th row in the fbphotos table and the photo name was like every other one that came before and after it so its not caused by some type of special character or anything making it end abruptly.

    What could I be missing?
     
    Stealthrtt, Jan 8, 2022 IP
  2. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #2
    Why can't you do a regular select and then create the json yourself?
     
    JEET, Feb 8, 2022 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #3
    I suspect the group concat has a limit - someone has probably documented it somewhere and 13 seems a reasonable cutoff.

    I come from the school of thought where you get the database to do as much of the heavy lifting as possible.

    This is not one of those times.

    Every serverside script, including whichever one you're using, will have a json_encode function to convert an array into JSON. Use it.
     
    sarahk, Feb 8, 2022 IP
    JEET likes this.