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!!

SUNIL
  • 107
  • 1
  • 10

2 Answers2

1

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).

Community
  • 1
  • 1
NID
  • 3,238
  • 1
  • 17
  • 28
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);

?>

Rendy Eko Prastiyo
  • 1,068
  • 7
  • 16
  • This code is not working.Still im encountering the same problem @rendy – SUNIL Feb 16 '17 at 08:05
  • No.Nothing,I think there might be some error in the jquery.Im unable to figure it out. – SUNIL Feb 16 '17 at 08:08
  • Try debugging the jquery, like adding `console.log(data);` inside `success: function (data) {}` – Rendy Eko Prastiyo Feb 16 '17 at 08:09
  • 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 !! – SUNIL Feb 16 '17 at 12:00