【发布时间】:2017-02-14 05:45:38
【问题描述】:
我正在使用 MySQL 并在其中创建了 2 个表; Users 和 Activity。用户表有以下数据:
目前我只使用上述数据。我希望这些数据以 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 的“连接成功”。
我不知道我做错了什么。
【问题讨论】: