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 8002545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:23:45+00:00 2026-06-04T16:23:45+00:00

I want to return JSON data from a resulted SQL statement in a PHP

  • 0

I want to return JSON data from a resulted SQL statement in a PHP script upon pressing Submit button, but I receive null instead.

I’ll be using the returned JSON to filter-show markers on my Google Map, but for now I just want to get the data back across to my jQuery page from PHP script so I can manipulate/use it.

Submit button:

HTML

<input type="submit" id="filter" value="Filter" />

JS

$('#myform').on('submit', function(e) {
  e.preventDefault();
  var myData = $('#myform').serializeArray();
  $.getJSON('myscript.php', myData, function(json){
    alert(json);// actually filter for later                    
  });   
});

PHP script:

// action is a hidden form control I use to check if form was submitted
    if(isset($_POST["action"])){

        if(isset($_POST["color"]) && isset($_POST["zipcode"])){
            // try to open a connection to a MySQL server
            $connection = mysql_connect($host, $username, $password) or die("Could not connect" . mysql_error());
            // select the active MySQL database to work with
            $db_selected = mysql_select_db($database, $connection) or die("Can\'t use db:" . mysql_error());

            $query = 'sql statement to return resutls based on what color and zipcode was provided';
            $result = mysql_query($query) or die("Can\'t do that: " . mysql_error());
        }

        //  close connection to the database


    echo json_encode($result);
    mysql_close($connection);
    }
  • 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-04T16:23:46+00:00Added an answer on June 4, 2026 at 4:23 pm

    You can’t return the result object of a mysql_query call directly. You first have to parse it with functions like mysql_fetch_array or alike (PHP docu).

    ...
    $result = mysql_query($query);
    if ( $result === false ) {
      die("Can\'t do that: " . mysql_error());
    }
    
    $retVal = array();
    while( $row = mysql_fetch_array( $result ) ) {
      $retVal[] = $row;
    }
    
    ...
    echo json_encode( $retVal );
    

    EDIT

    According to the jQuery spec for getJSON (link), the data is sent using GET parameters and not using POST. So you would have to change all the $_POST appearances in your PHP code to either $_GET or $_REQUEST.

    Besides this, you should return some error messages if your variables are not set. Right now (according to your code) just an empty document is returned.

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

Sidebar

Related Questions

I want to return a json object from the onreadystatechange = function(){}, but I
I want use php curl with oauth to get the JSON data from twitter
I want to return the Model (data) of a controller in different formats (JavaScript/XML/JSON/HTML)
I want to return a private field from a remoted object but i get
I want to send a large amount of cached json data from an import
I am newer for php. I want make php page cache, query data from
I have a php script that fetches data from a DB, the result set
I made a script to get JSON data from a file on our server
I want to send json data from my asp.net MVC controller in C# and
I just simply want to return a JSON object (using ajax) from my server

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.