dimanche 26 juin 2016

mysqli_stmt_fetch() Not Working

I am trying to save a value from a mysqli_stmt_fetch() statement. When my application is run, it returns No Value for this variable. I am new to PHP and cannot fully debug this file. Where is the bug at?

My php file:

<?php
    require("password.php");

    $connect = mysqli_connect("website", "account", "my_pass", "db");

    $name = $_POST["name"];
    $theme = $_POST["theme"];
    $username = $_POST["username"];
    $email = $_POST["email"];
    $defaultRadius = $_POST["radius"];
    $password = $_POST["password"];

    function registerUser() {
        global $connect, $name, $username, $theme, $email, $defaultRadius, $password;
        $passwordHash = password_hash($password, PASSWORD_DEFAULT);
        $statement = mysqli_prepare($connect, "INSERT INTO user (name, username, theme, email, default_radius, password) VALUES (?, ?, ?, ?, ?, ?)");
        mysqli_stmt_bind_param($statement, "ssssss", $name, $username, $theme, $email, $defaultRadius, $passwordHash);
        mysqli_stmt_execute($statement);
        mysqli_stmt_bind_result($statement, $colUserID, $colName, $colUsername, $colTheme, $colEmail, $colDefaultRadius, $colPassword);
        while(mysqli_stmt_fetch($statement)){
            $response["userId"] = $colUserID;
        }
        mysqli_stmt_close($statement);
    }

    function usernameAvailable() {
        global $connect, $username;
        $statement = mysqli_prepare($connect, "SELECT * FROM user WHERE username = ?"); 
        mysqli_stmt_bind_param($statement, "s", $username);
        mysqli_stmt_execute($statement);
        mysqli_stmt_store_result($statement);
        $count = mysqli_stmt_num_rows($statement);
        mysqli_stmt_close($statement); 
        if ($count < 1){
            return true; 
        } else {
            return false; 
        }
    }

    $response = array();
    $response["success"] = false;  
    $response["reason"] = 0;

    if (usernameAvailable()){
        registerUser();
        $response["success"] = true;
    } else {
        $response["reason"] = 1;
    }

    echo json_encode($response);
?>

The variable that I am trying to set is located inside the registerUser function. It states:

while(mysqli_stmt_fetch($statement)){
    $response["userId"] = $colUserID;
}

Thanks for any help!

Edit: My new/current code is as follows:

<?php
    require("password.php");

    $connect = mysqli_connect("xenicdev.x10host.com", "xenicdev_root", "shadow1", "xenicdev_data");

    $name = $_POST["name"];
    $theme = $_POST["theme"];
    $username = $_POST["username"];
    $email = $_POST["email"];
    $defaultRadius = $_POST["radius"];
    $password = $_POST["password"];

    function registerUser() {
        global $connect, $name, $username, $theme, $email, $defaultRadius, $password, $response;
        $passwordHash = password_hash($password, PASSWORD_DEFAULT);
        $statement = mysqli_prepare($connect, "INSERT INTO user (name, username, theme, email, default_radius, password) VALUES (?, ?, ?, ?, ?, ?)");
        mysqli_stmt_bind_param($statement, "ssssss", $name, $username, $theme, $email, $defaultRadius, $passwordHash);
        mysqli_stmt_execute($statement);
        mysqli_stmt_bind_result($statement, $colUserID, $colName, $colUsername, $colTheme, $colEmail, $colDefaultRadius, $colPassword);
        while(mysqli_stmt_fetch($statement)){
            return $colUserID;
        }
    }

    function usernameAvailable() {
        global $connect, $username;
        $statement = mysqli_prepare($connect, "SELECT * FROM user WHERE username = ?"); 
        mysqli_stmt_bind_param($statement, "s", $username);
        mysqli_stmt_execute($statement);
        mysqli_stmt_store_result($statement);
        $count = mysqli_stmt_num_rows($statement);
        mysqli_stmt_close($statement); 
        if ($count < 1){
            return true; 
        } else {
            return false; 
        }
    }

    $response = array();
    $response["success"] = false;  
    $response["reason"] = 0;

    if (usernameAvailable()){
        $userId = registerUser();
        $response["userId"] = $userId;
        $response["success"] = true;
    } else {
        $response["reason"] = 1;
    }

    echo json_encode($response);
?>

It returns null as "userId" instead of the ID though... Please note the ID is not null in the SQL Database. In my testing case, the ID is 8.

StringRequest code used to call this PHP file from Android:

public class RegisterRequest extends StringRequest {

    private static final String REGISTER_REQUEST_URL = "http://xenicdev.x10host.com/Register.php";
    private Map<String, String> params;

    public RegisterRequest(String name, String username, int themeId, String password, String email, int defaultRadius, Response.Listener<String> listener) {
        super(Method.POST, REGISTER_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("name", name);
        params.put("username", username);
        params.put("theme", themeId + "");
        params.put("email", email);
        params.put("radius", defaultRadius + "");
        params.put("password", password);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

Aucun commentaire:

Enregistrer un commentaire