2016-05-26 60 views
0

我需要從單個列中檢索數據並將其放入API(Json)中,但出於某種原因,我也從列中獲取標題。檢索單個列的值

$sql = "SELECT workingJson FROM dataTable"; 

我認爲它會像workingJson.Value,但沒有運氣。

這裏是API.php根據您的意見

// Create connection 
$con=mysqli_connect("localhost","user","password","database"); 

// Check connection 
if (mysqli_connect_errno()) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

// This SQL statement selects ALL from the table 'Locations' 
$sql = "SELECT column1 FROM database"; 

// Check if there are results 
if ($result = mysqli_query($con, $sql)) 
{ 
    // If so, then create a results array and a temporary one 
    // to hold the data 
    $resultArray = array(); 
    $tempArray = array(); 

    // Loop through each row in the result set 
    while($row = $result->fetch_object()) 
    { 
     // Add each row into our results array 
     $tempArray = $row; 
     array_push($resultArray, $tempArray); 
    } 

    // Finally, encode the array to JSON and output the results 
    echo json_encode($resultArray); 
} 

// Close connections 
mysqli_close($con); 

回答

1

編輯:

要只返回在PHP中的值,而不是鑰匙,你可以在你的代碼改成這樣:

// Loop through each row in the result set 
while($row = $result->fetch_object()) 
{ 
    // Add value to array 
    array_push($resultArray, $row->column1); 
} 

在這種情況下,您不需要$tempArray

+0

@WillemMassoels我已經編輯按你的意見我的回答。 –

+0

恐怕沒有運氣,謝謝麥克! –

+0

@WillemMassoels我認爲你需要提供更多的調試信息。我所展示的將返回一個JSON表示,如'[「somevalue」,「someothervalue」,...]'當你說「沒有運氣」時,這意味着什麼?在您的服務器端或客戶端代碼執行中,執行的執行方式與您所期望的不同? –

0

你可以得到所有的結果和做traitment後:

<?php 

// Create connection 
$con=mysqli_connect("localhost","user","password","database"); 

// Check connection 
if (mysqli_connect_errno()) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

// This SQL statement selects ALL from the table 'Locations' 
$sql = "SELECT column1 FROM database"; 


if ($result = $mysqli->query($sql)) { 

    //get all fields : 
    $finfo = $result->fetch_fields(); 

    foreach ($finfo as $val) { 
     $resultArray[] = $val->column1; 
    } 
    $result->close(); 
} 

// Finally, output the results without encode because is also in json format: 
echo '{'. implode(','.chr(10), $resultArray) .'}'; 

$mysqli->close(); 
+0

謝謝Ilyas,​​是echo($ resultArray); $ mysqli-> close();用純文本回顯數組的正確語法......目前我只是得到「Array」作爲結果。 –

+0

oooh是的,我忘記了這一點,現在嘗試與內爆! – Mimouni

+0

謝謝伊利亞斯的全力幫助。 –