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.
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.
Add this line After sql Query
$sql="insert sql query";
$res=mysqli_query($con,$sql);
Add in while loop use this one
$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);
?>
Put config and php file in the same folder