I am using MySQL and created 2 tables in it; Users
and Activity
. User table has following data:
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.