6
zuley
73d

I cant get a JSON object out of my query results!! I have been trying all day and I'm so frustrated and sad. I'm new to JavaScript, AJAX, and JSON. I just want to understand this. I've seen videos, tutorials, but I never get the expected results with jason_encode. And on the client side I don't even know if the request is good since I can't make the JSON object. :(

Comments
  • 3
    I have nothing but feels for you, man. I’ve been living this for like two weeks.
  • 2
    @broseph thank you for the feels at least. Currently taking a break before I go back to tackle this. Hoping we both figure this out.
  • 2
    @zuley

    I very highly recommend using a REST client like insomnia to test it, as it will cut through all the http handling for you and just show you what you need.
  • 1
    @bahua Ok I'll take your advice. I've never used it before so it may just add to my confusion but I'll look up some videos. Thanks!
  • 4
    Or postman, understand js objects and array first thrn it’s easier to read JSON
  • 1
    @devTea I really appreciate your suggestion. I looked up both clients and I kind of get it but I think I need to take a step back. Maybe I'm not handling the query results right. When I echo json_encode($query_results) what prints is the actual query string (SELECT FROM WHERE etc.). I feel like that shouldn't happen...? Also when I use fetchAll() nothing happens. I'm looking at w3schools website and they are using ->prepare() and ->execute() and ->get_results() which I am not. Is this where I'm going wrong? The way I am running the query is $results = $db->query($query); return $results. To print the results I am using a foreach loop in a printResults function and they print okay in a table format. But they dont seem to get passed to the json object. I am not looping when I am trying to create the object (in a separate function) bc I thought fetchAll() would do that, but as I said that doesn't work. Sorry for the long post but thanks for reading.
  • 2
    @zuley you'll want to json_encode the result of fetch_all probably
  • 1
    @White I did try this once I set $rows = fetchAll($query_results) ... json_encode($rows) ... when echo this last statement all that returns is brackets like this []

    Thanks for your suggestion. I'll definitely post an update when I figure this out.
  • 1
    @zuley that means the SQL query returned zero rows
  • 1
    @White but when I use a printResults function it prints all the results. Does it have to do with looping through the results? Also before trying to encode I use rowCount() to check if any rows returned.
  • 2
    @zuley it's possible you're consuming the rows before the call to fetch_all, but it's hard to tell without looking at the code. I'm sure you'd get some good help here if you linked to a hastebin of the code snippet, but I'm gonna get some shut-eye.
    Good luck with the debugging!
  • 2
    @White Ah I am quite new so I didnt know I could link. I'm finding the devRant community really supportive though. Thanks again for the suggestions. x
  • 1
    @White

    Wow what you said was right! The printResults was "consuming" the results data before fetchAll(). I did not know this was possible. After removing the printResults call and only using fetchAll and json_encode my data is now being encoded into a json object. Thank you so so much.
  • 2
    "i just want to understand this"

    there's your problem.

    repeat after me: "Javascript is not understood nor written. Javascript is referenced from other people's online repos that you have no control over, or, if you must, copypasted from random google search results."
Your Job Suck?
Get a Better Job
Add Comment