1
<?php
    include("db_connection.php");
      if(isset($_POST['id']) && isset($_POST['id']) != "")
 {
// get User ID
$user_id = $_POST['id'];

// Get User Details
$query = "SELECT * FROM products WHERE id = '$user_id'";
if (!$result = mysql_query($query)) {
    exit(mysql_error());
}
$response = array();
if(mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
        $response[] = $row;         
    }
}
else
{
    $response['status'] = 200;
    $response['message'] = "Data not found!";
}   
// display JSON data
header('Content-type: application/json');
   echo json_encode($response); ;   

}
else
{
$response['status'] = 200;
$response['message'] = "Invalid Request!";
}
?>

The above code gets a value from the home page.Fetches the row from database and passes the row to home page using json.The echo json_encode($response) is not printing the json value.Is the array assigned to $response? What are the changes i need to make? Help me out!!

6
  • Did you enable error messages? Any shown when called directly? Commented Feb 16, 2017 at 7:15
  • in else condition you have kept json_reponse success and message it wont come.. Commented Feb 16, 2017 at 7:15
  • It doesnt show any errors @mplungjan Commented Feb 16, 2017 at 7:21
  • where hav i given as success? @Sona Commented Feb 16, 2017 at 7:29
  • if(mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { $response[] = $row; } } else { $response['status'] = 200; $response['message'] = "Data not found!"; } Commented Feb 16, 2017 at 7:33

2 Answers 2

2

json_encode requires all data you feed in to be UTF-8 encoded and it will fail otherwise. See UTF-8 all the way through for how to get UTF-8 encoded data out of your database (use mysqli_set_charset).

Sign up to request clarification or add additional context in comments.

Comments

1

Jquery:

$.ajax({
    url: '[YOUR_AJAX_CONTROLLER_URL]',
    data: {
        format: 'json'
    },
    success: function (data) {
        if (data.status == 'success') {
            // your action when response status is success.
            // the user details are in data.items
        } elseif (data.status == 'error') {
            // your action when response status is error.
            // the error message is in data.message
        }
    },
    error: function () {
        // your action when request is not accepted.
        // there are no data from your PHP,
        // because this is server error, not PHP error.
    }
});

PHP:

<?php
    include("db_connection.php");

    if (isset($_POST['id']) && $_POST['id'] != "") {
        // get User ID
        $user_id = $_POST['id'];

        // Get User Details
        $query = "SELECT * FROM products WHERE id = '$user_id'";

        $response = array();

        if (!$result = mysql_query($query)) {
            $response = [
                'status' => 'error',
                'message' => mysql_error(),
            ];
        } else if (mysql_num_rows($result) > 0) {
            while ($row = mysql_fetch_assoc($result)) {
                $response = array(
                    'status' => 'success',
                    'item' => $row,
                );
            }
        } else {
            $response = [
                'status' => 'error',
                'message' => 'Data not found!',
            ]
        }   
    } else {
        $response = [
            'status' => 'error',
            'message' => 'Invalid Request!',
        ];
    }

    // display JSON data
    header('Content-type: application/json');
    echo json_encode($response);

?>

4 Comments

This code is not working.Still im encountering the same problem @rendy
No.Nothing,I think there might be some error in the jquery.Im unable to figure it out.
Try debugging the jquery, like adding console.log(data); inside success: function (data) {}
The code worked. It wasnt working because there was image file in my database and image files donot encode using json.i converted the images to base64 manually and then tried json,it worked perfectly.Thank you all !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.