簡體   English   中英

如何在php中從mysql編碼多個結果…我得到5個結果,但是當我運行API時,它沒有以JSON格式顯示任何數據

[英]How to encode multiple results from mysql in php…I am getting 5 results but when i run the API it doesn't show any data as JSON format

我在php中創建了一個API以從mysql數據庫中搜索,數據庫給了我5個結果,但是在我運行它的php API中...它沒有顯示任何數據,因為下面的響應是我的API代碼。

這是我的Php的API,我將包名作為標頭中的輸入,然后在mysql數據庫中找到該輸入,這應返回5個結果給我,而且我必須將這5個結果編碼為json並作為響應發送。

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "package";

//Create Connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check Connection
if($conn->connect_error) {
    die("Connection Failed: " . $conn->connect_error);
}

$packagename = (print_r($_SERVER['HTTP_PACKAGENAME'],true));
//$pass = (print_r($_SERVER['HTTP_PASSWORD'],true));
//print_r($_SERVER);

//print_r(apache_request_headers());
$sql = "select * from  `package_details` where Package_Name='$packagename' ";
$result = $conn->query($sql);

if($result->num_rows>0) {
    //$response["package_details"] = array();
    //Output data of each row
    while($row = $result->fetch_assoc()) {
        //temp user array
        $user = array();

        $user["id"] = $row["id"];
        $user["Package_Name"] = $row["Package_Name"];
        $user["Package_Day"] = $row["Package_Day"];
        $user["Package_Description"] = $row["Package_Description"];

        //push single product into final response array
        //array_push($response["package_details"], $user);
    }
    //success
    //$response["success"] = "valid";
    //echoing JSON response
    echo json_encode($user);
} else {
    //no products found
    $response["package_details"] = array();
    while($row = null) {

    $user = array();
        $user["id"] = $row[null];
        $user["Name"] = $row[null];
        $user["Contact_Number"] = $row[null];
        $user["Email_Id"] = $row[null];


        //push single product into final response array
        array_push($response["package_details"], $user);
    }
        $response["success"] = "invalid";
    //$response["success"] = "invalid";
    //$response["message"] = "No Products Found";

    //echo no users JSON
    echo json_encode($response);
}
?>

您只是對最后一個$user進行json編碼,而不是應放入$response['package_details']的用戶數組

請參閱下面的修改后的代碼

$response = array();

if($result->num_rows>0) {

    while($row = $result->fetch_assoc()) {
        $user = array();

        $user["id"]                  = $row["id"];
        $user["Package_Name"]        = $row["Package_Name"];
        $user["Package_Day"]         = $row["Package_Day"];
        $user["Package_Description"] = $row["Package_Description"];

        //push single product into final response array
        $response["package_details"][] = $user;
    }
    //success
    $response["success"] = "valid";

} else {

    //no products found
    $response["success"] = "invalid";
    $response["message"] = "No Products Found";
}

echo json_encode($response);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM