我需要从单个列中检索数据并放入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);
答案 0 :(得分: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
。
答案 1 :(得分:0)
你可以在做完特质后得到所有结果:
<?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();