2016-05-26 34 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

谢谢伊利亚斯的全力帮助。 –