Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9132637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:20:41+00:00 2026-06-17T08:20:41+00:00

Im new to PHP and trying to make a very simple api (good way

  • 0

Im new to PHP and trying to make a very simple api (good way too learn i think), why doesnt this work?
When loading this page the output is: {“queryString”:”SELECT id, Name FROM Food WHERE userID=1 ORDER BY Name”} and not the JSON-string that I want

$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);

$response = $dbh->query("SELECT id, Name FROM Food WHERE userID=1 ORDER BY Name");

$responseJSON = json_encode($response);

header('Content-type: application/json');
echo "$responseJSON";

$dbh = null;

Nothing is wrong with the query because i get what i want when trying this:

$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);

$response = $dbh->query("SELECT id, Name FROM Food WHERE userID=1 ORDER BY Name");

    foreach ($response as $row)
        {
            print $row['Name'] . ' ';
        }


$dbh = null;

outputs:
Bacon
Cheese
chips

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:20:42+00:00Added an answer on June 17, 2026 at 8:20 am

    $dbh->query retuns a PDOStatement object, not the rows. To add the rows to the json, call fetchAll on the object.

    $response = $dbh->query("SELECT id, Name FROM Food WHERE userID=1 ORDER BY Name")->fetchAll();
    

    You may also specify the fetch mode, e.g.

    fetchAll(PDO::FETCH_ASSOC);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to php and am trying to make a very simple two page
I am pretty new to PHP and I am trying to make an inventory
I am new to PHP and trying to learn it by creating a database
I'm new to php and I'm trying to create a simple example for calling
I am new to php and I am getting this error trying to load
I am new to PHP and JavaScript development, I am trying to execute this
This will sound very noob, but I'm new to HTML/javascript/php, and so far I've
I am trying to make a simple upload file system, and this is my
I'm trying to make a very basic comment system in PHP. The problem is
I am very new to Linux but I am trying best to learn. We

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.