Akshay
Akshay

Reputation: 47

PHP Not Passing Correct JSON Result

Hello All I have made a php for my android application project which creates a json containing the returned data from database but the problem is that it is not returning anything except "[]" square brackets.

FetchUserData.php

<?php

$con=mysqli_connect("mysql8.000webhost.com" , "====" , "passwordGoesHere" , "=====");

$password = $_POST["password"];
$username = $_POST["username"];

$statement = mysqli_prepare($con, "SELECT * FROM User WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss" , $username, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement ,$userID , $name , $age , $username , $password);

$user = array();

while(mysqli_stmt_fetch($statement)){
    $user[name] = $name;
    $user[age] = $age;
    $user[username] = $username;
    $user[password] = $password;
}

echo json_encode($user);
mysqli_stmt_close($statement);
mysqli_close($con);
?>

Upvotes: 0

Views: 101

Answers (2)

user3046565
user3046565

Reputation: 1

check your sql query whether it is returning any result or not. I think your query is not giving any result. Hence you are getting blank array. Run your query directly into database & check whether you are getting anything

Upvotes: 0

Evadecaptcha
Evadecaptcha

Reputation: 1441

It's because you're using what the php parser interprets as undefined constants. You're not wrapping your keys in quotations. Should be:

while(mysqli_stmt_fetch($statement)){
    $user['name'] = $name;
    $user['age'] = $age;
    $user['username'] = $username;
    $user['password'] = $password;
}

Upvotes: 1

Related Questions