【问题标题】:Unable to view JSON data from PHP file无法从 PHP 文件中查看 JSON 数据
【发布时间】:2017-02-14 05:45:38
【问题描述】:

我正在使用 MySQL 并在其中创建了 2 个表; UsersActivity。用户表有以下数据:

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


( ! ) 警告: mysqli_query() 至少需要 2 个参数,其中 1 个在 D:\xampp\htdocs\MobileApp\index.php 在 18 行 称呼 堆栈#TimeMemoryFunctionLocation 10.0015134480{main}( )...\index.php:0 20.0034141920getUserName( )...\index.php:38 30.0034142272http:// www.php.net/function.mysqli-查询' target='_new'>mysqli_query ( )...\index.php:18

( ! ) 警告: mysqli_fetch_array() 期望参数 1 为 mysqli_result, 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='_new'>mysqli_fetch_array ( )...\index.php:20

{"users":[]}

我已经分 3 个步骤发布了上述结果,因此可以清楚地确定我犯了什么错误。

下面是我的 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";
 }

我收到来自config 的“连接成功”。

我不知道我做错了什么。

【问题讨论】:

    标签: php mysql json


    【解决方案1】:

    问题出在你使用mysqli_query的方式上。

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

    即:

    $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_query($con, "SELECT * FROM Users"); 中添加$con 时出现错误unidentified variable con
    • 您确定包含正确的连接文件吗?
    • 我正在使用一个函数,所以有什么关系吗?
    • 如果您尝试直接访问 config.php 会怎样?您是否看到“连接成功”字符串或 mysqli_connection 错误?
    • 像冠军一样工作:)
    【解决方案2】:

    在sql查询后添加这一行

    $sql="insert sql query";
    
    $res=mysqli_query($con,$sql);
    

    在while循环中添加使用这个

    $row=mysqli_fetch_array($res)
    

    更新

        <?php
    require_once ('config.php');
    
    $result= "SELECT * FROM Users";
    $res=mysqli_query($con,$result);
    
    $response = array();
    
    while ($row=mysqli_fetch_array($res)){
            array_push($response,array('Id' =>$row['Id'] ,
        'Name' =>$row['Name']
    
            //      add element on your array
    
        ));
        }
        echo json_encode(($response));
    
        mysqli_close($con);
    
    ?>
    

    将配置文件和php文件放在同一个文件夹中

    【讨论】:

    • $result = mysqli_query($con, "SELECT * FROM Users"); 中添加$con 时出现错误unidentified variable con
    • 在配置文件中添加:$con = mysqli_connect(HOST,USERNAME,PASSWORD,DATABASE)or die('Unable to Connect'); 并删除 if else 循环
    • 请在我的问题中查看我的config 部分,我已经添加了它
    • 我必须排除我的功能吗?
    • 没有必要添加你的功能..但是如果你想要你可以
    猜你喜欢
    • 2021-02-12
    • 2017-04-20
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 2017-07-20
    • 2017-01-06
    相关资源
    最近更新 更多