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

无法查看PHP文件中的JSON数据,php,mysql,json,Php,Mysql,Json,我正在使用MySQL,并在其中创建了2个表;用户和活动。用户表包含以下数据: 目前我仅使用上述数据。我希望这些数据以JSON格式显示,然后我将在我的Android应用程序中使用它。我已尝试将其转换,但得到以下结果: !!警告: mysqli_查询至少需要2个参数,其中1个在 第18行的D:\xampp\htdocs\MobileApp\index.php 呼叫 堆栈TimeMemoryFunctionLocation 10.0015134480{main}…\index.php:0 20.003

我正在使用MySQL,并在其中创建了2个表;用户和活动。用户表包含以下数据:

目前我仅使用上述数据。我希望这些数据以JSON格式显示,然后我将在我的Android应用程序中使用它。我已尝试将其转换,但得到以下结果:

!!警告: mysqli_查询至少需要2个参数,其中1个在 第18行的D:\xampp\htdocs\MobileApp\index.php 呼叫 堆栈TimeMemoryFunctionLocation 10.0015134480{main}…\index.php:0 20.0034141920getUserName…\index.php:38 30.0034142272http://www.php.net/function.mysqli-query' target='''\u new'>mysqli\u query…\index.php:18

!!警告: mysqli_fetch_数组期望参数1为mysqli_结果,null 在第行的D:\xampp\htdocs\MobileApp\index.php中给出 20调用堆栈TimeMemoryFunctionLocation 10.0015134480{main}…\index.php:0 20.0034141920getUserName…\index.php:38 30.0050142240http://www.php.net/function.mysqli-fetch-array' target=''\u new'>mysqli\u fetch\u数组…\index.php:20

我已经发布了上述三个步骤的结果,以便可以清楚地确定我犯了什么错误

下面是我的PHP文件:

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();
和我的配置文件:

<?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";
 }
我正在从配置中获取“连接成功”


我不知道我做错了什么

在sql查询之后添加此行

$sql="insert sql query";

$res=mysqli_query($con,$sql);
外接程序while循环使用此

$row=mysqli_fetch_array($res)
更新
将config和php文件放在同一个文件夹中

问题来自使用mysqli\u查询的方式

正如错误消息中所说,mysqli_查询需要两个参数。第一个是连接对象,第二个是查询

i、 e:

$result = mysqli_query($con, "SELECT * FROM Users");
然后,使用json_encode php函数构建如下json数据

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();

希望能有所帮助。

在$result=mysqli\u query$con中添加$con时,选择*FROM Users;它给了我一个未识别的错误变量CON你确定你包含了正确的连接文件吗?我正在使用一个函数,所以有什么关系吗?如果你尝试直接访问config.php呢?您是否看到连接成功字符串或mysqli_连接错误?像champ一样工作:在$result=mysqli_查询$con中添加$con时,从用户中选择*;它给了我一个未识别的错误变量conadd this在你的配置文件中:$con=mysqli_connectHOST,用户名,密码,数据库或die'canable to Connect';如果还有,请查看我的问题中的配置部分我已经添加了它我必须排除我的函数吗?没有必要添加你的函数。。但是如果你想,你可以
$result = mysqli_query($con, "SELECT * FROM Users");
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();