dongweiben5229 2016-05-26 16:33
浏览 35
已采纳

检索单个列的值

I need to retrieve data from a single column and put in an API (Json), but for some reason I get the title from the column as well.

$sql = "SELECT workingJson FROM dataTable";

I assumed it would be like workingJson.Value, but no luck.

Here is the 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);
  • 写回答

2条回答 默认 最新

  • douqi0090 2016-05-26 16:39
    关注

    Edit per your comments:

    To just return the values and not the keys in PHP, you could change your code to this:

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

    You have no need for $tempArray in this case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题