2017-02-16 25 views
0
<?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!"; 
} 
?> 

を印刷doesntの上記のコードは、データベースからホームpage.Fetches列から値を取得し、json.Theを使用してホーム・ページに行を通過エコーjson_encode($応答)でありますjsonの値を表示しません。$ responseに配列が割り当てられていますか?私が必要とする変更は何ですか?私を助けてください!json_encodeが動作していない、それはJSON値を

+0

あなたはエラーメッセージを有効にしましたか?直接呼び出されたときに表示されますか? – mplungjan

+0

それ以外の条件では、json_reponseの成功とメッセージは届きません。 – Sona

+0

エラーが表示されません@mplungjan – SUNIL

答えて

1

json_encodeは、フィードするすべてのデータをUTF-8にエンコードする必要があり、それ以外の場合は失敗します。 UTF-8エンコードされたデータをデータベースから取得する方法についてはUTF-8 all the way throughを参照してください(mysqli_set_charsetを使用してください)。

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

>

+0

このコードは動作しません。同じ問題が発生しました@rendy – SUNIL

+0

エラーがありますかメッセージ? –

+0

いいえ、jqueryに何らかのエラーがあると思われます。わかりません。 – SUNIL

関連する問題