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

  • Home
  • SEARCH
  • 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 3313866
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:09:55+00:00 2026-05-17T22:09:55+00:00

I have to return data fetched from MySQL table into a php file as

  • 0

I have to return data fetched from MySQL table into a php file as JSON. Here is my code to connect to mysql & get data from it. How could now I return it as JSON.

<?php
    $username = "user";
    $password = "********";
    $hostname = "localhost";    
    $dbh = mysql_connect($hostname, $username, $password) 
        or die("Unable to connect to MySQL");
    print "Connected to MySQL<br>";
    $selected = mysql_select_db("spec",$dbh) 
        or die("Could not select first_test");
    //$rows = array();  

    $query = "SELECT * FROM user_spec"; 
    $result=mysql_query($query);

    //mysql_close($dbh);
    ?>

Under is full stack that I have to implement. For step 3, I am rendering the list dynamically using user inputs though its not using any engine but directly using input values so I have to see it how to do once I get JSON data. I put the stack so that you people can kindly see it what I have to do when possibly helping me.

  1. the user load an HTML page
  2. the page make an ajax call and get the options as a JSON(either it exists already in the database, or a new option set is generated)
  3. the json is rendered using a JS templating engine (PURE in our case)
  4. the user change something
  5. the same JSON is modified and sent by a POST to the server
  6. the server read that JSON and store it in the database(you would write the data to your file). And then go back to the step 4, to wait for another user change.
  • 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-05-17T22:09:55+00:00Added an answer on May 17, 2026 at 10:09 pm

    Given that only one user_spec row is returned you can use the built in json_encode function:

    <?php
    $username = "user";
    $password = "********";
    $hostname = "localhost";    
    $dbh = mysql_connect($hostname, $username, $password) 
        or die("Unable to connect to MySQL");
    
    //print "Connected to MySQL<br>";
    
    $selected = mysql_select_db("spec",$dbh) 
        or die("Could not select first_test");
    
    $query = "SELECT * FROM user_spec"; 
    $result=mysql_query($query);
    
    echo json_encode(mysql_fetch_assoc($result));
    
    ?>
    

    Should do the trick.

    Even if you are using an older version of PHP, you can find a suitable function in the user comments at the json_encode PHP Manual page to use in it’s place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page where I submit some data, and return to the original
Say I have a stored procedure that returns data from a SELECT query. I
I have noticed that cURL in PHP returns different data when told to output
I have a data tier select method that returns a datatable. It's called from
I just realized that in some place in my code I have the return
Most program languages have some kind of exception handling; some languages have return codes,
I was surprised recently to find that it's possible to have a return statement
I have problem with return statment >.< I want to store all magazine names
Is there a way to have a function return a editable reference to some
Are there good reasons why it's a better practice to have only one return

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.