duangangmo0583 2017-02-14 05:45
浏览 40
已采纳

无法从PHP文件中查看JSON数据

I am using MySQL and created 2 tables in it; Users and Activity. User table has following data:

enter image description here

For now I am using the above data only. I want this data to show in JSON format and then I will use it in my Android app. I have tried to convert it but I am getting the below result:


( ! ) Warning: mysqli_query() expects at least 2 parameters, 1 given in D:\xampp\htdocs\MobileApp\index.php on line 18 Call Stack #TimeMemoryFunctionLocation 10.0015134480{main}( )...\index.php:0 20.0034141920getUserName( )...\index.php:38 30.0034142272http://www.php.net/function.mysqli-query' target='_new'>mysqli_query ( )...\index.php:18

( ! ) Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in D:\xampp\htdocs\MobileApp\index.php on line 20 Call Stack #TimeMemoryFunctionLocation 10.0015134480{main}( )...\index.php:0 20.0034141920getUserName( )...\index.php:38 30.0050142240http://www.php.net/function.mysqli-fetch-array' target='_new'>mysqli_fetch_array ( )...\index.php:20

{"users":[]}

I have posted the above result in 3 steps so it can be clearly identified what mistake I am making.

Below is my PHP file:

require_once ('config.php');

function getUserName()
{
// array for json response
$response = array();
$response["users"] = array();

// Mysql select query
$result = mysqli_query("SELECT * FROM Users");

while ($row = mysqli_fetch_array($result))
{
    // temporary array to create single category
    $tmp = array();
    $tmp["Id"] = $row["Id"];
    $tmp["Name"] = $row["Name"];

    // push category to final json array
    array_push($response["Users"], $tmp);
}
// keeping response header to json
header('Content-Type: application/json');

// echoing json result
echo json_encode($response);
}

getUserName();

And my config file:

<?php 
 define("HOST","localhost");
 define("DATABASE","app");
 define("USERNAME","root");
 define("PASSWORD","");

 $con=mysqli_connect(HOST,USERNAME,PASSWORD,DATABASE);
 if(!$con){
    die("Database Connection Error: " . mysqli_connect_error());
 }
 else{
  echo "Connection successful";
 }

I am getting ‘Connection successful’ from config.

I don't know what wrong I am doing.

  • 写回答

2条回答 默认 最新

  • duanshang9426 2017-02-14 05:59
    关注

    The problem comes from the way you use mysqli_query.

    As said in error message mysqli_query expects two parameters. The first one will be the connection object and the second one, the query.

    i.e. :

    $result = mysqli_query($con, "SELECT * FROM Users");
    

    And then, build you json data like this with json_encode() php function.

    require_once ('config.php');
    
    function getUserName()
    {
        // defines global since $con not in the scope of the function variables
        global $con;
    
        $response = array();
        while ($row = mysqli_fetch_array($result))
        {
            // temporary array to create single category
            $tmp = array();
            $tmp["Id"] = $row["Id"];
            $tmp["Name"] = $row["Name"];
    
            // build response array
            $response['users'][] = $tmp;
        }
    
        // convert $response array to json and return it
        return json_encode($response);
    }
    
    // get function return 
    $users_name = getUserName();
    

    Hope it helps.

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

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错