2016-09-17 2 views
0

LAMP 서버에서 제품 데이터를 가져 오려고합니다. 그러나 나는 응답을 얻지 않는다. 문제는 array_push가 echo를 사용하기 전에 데이터를 인쇄 할 수 있기 때문에 제대로 작동하지 않는 것 같습니다.php : 배열에 값을 할당 할 수 없어서 respone으로 전달할 수 없습니다.

나는 PHP에 상당히 익숙하다.

미리 도움을 주셔서 감사합니다.

<?php 
ini_set('display_errors', 'On'); 
error_reporting(E_ALL); 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "DB"; 

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

$sql = "SELECT * FROM Produkt"; 
$result = $conn->query($sql); 


// check for empty result 
if (mysqli_num_rows($result) > 0) { 

    // looping through all results 
    // products node 
    $response["products"] = array(); 

    while ($row = mysqli_fetch_assoc($result)) { 
     // temp user array 
     $product = array(); 
     $product["ID"] = $row["ID"]; 
    $product["Name"] = $row["Name"]; 


     // push single product into final response array 
      //print json_encode($product); 
     array_push($response["products"], $product); 
    } 
    // success 
    $response["success"] = 1; 

    // echoing JSON response 
    echo json_encode($response); 
} else { 
    // no products found 
    $response["success"] = 0; 
    $response["message"] = "No products found"; 

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

하는 경우 : 원하는대로 자신의 코드를 만들 수 있습니다 당신은 데이터베이스에서'ID'와'Name'을 선택하기 만하면됩니다 (SQL을 변경하십시오). while 회 돌이에서'$ response [ 'products'] [] = $ row;'할 수 있습니다. – Rasclatt

답변

1
쉽게 간단한 코드를 달성 할 수

:

$i = 0; 
    while ($row = mysqli_fetch_assoc($result)) { 
     // temp user array 
     $product = array(); 
     $product["ID"] = $row["ID"]; 
    $product["Name"] = $row["Name"]; 


     // push single product into final response array 
      //print json_encode($product); 
     $response["products"][i] = $product; 
     $i++; 
    } 

또는 당신은뿐만 아니라 당신이

1

음주

처럼 somethink
<?php 
ini_set('display_errors', 'On'); 
error_reporting(E_ALL); 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "DB"; 

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

$sql = "SELECT * FROM Produkt"; 
$result = $conn->query($sql); 

$array = array(); 
// check for empty result 
if (mysqli_num_rows($result) > 0) { 

    // looping through all results 
    // products node 

    while ($row = mysqli_fetch_assoc($result)) { 
     // temp user array 
      $array['products'][] = $row; 
     // push single product into final response array 
      //print json_encode($product) 
    } 
    // success 
    $array["success"] = 1; 

    // echoing JSON response 
    echo json_encode($array); 
} else { 
    // no products found 
    $array["success"] = 0; 
    $array["message"] = "No products found"; 

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