mysqli_stmt_fetch()
문에서 값을 저장하려고합니다. 내 응용 프로그램을 실행하면이 변수에 No Value가 반환됩니다. 나는 PHP를 처음 사용하기 때문에이 파일을 완전히 디버그 할 수 없다. 어디에서 버그가 있습니까?mysqli_stmt_fetch()가 작동하지 않습니다.
내 PHP 파일 : 나는 registerUser의 함수 내에서 위치 설정하려고
<?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);
?>
변수. 상태 :
while(mysqli_stmt_fetch($statement)){
$response["userId"] = $colUserID;
}
어떤 도움을 주셔서 감사합니다!
는편집 :은 다음과 내 새/현재 코드가 될 때 :
<?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);
?>
그것은 ID는 SQL에서 null가 아닌 유의하시기 바랍니다 ...하지만 대신 ID의 "userId를"null를 돌려줍니다 데이터 베이스. 당신이 last insert
userID
필요한 경우
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;
}
}
가변 범위 문제. 함수 내에서'$ response'에 대한 값을 설정하고 있지만 전역 변수로 선언하거나 반환하지는 않습니다. – andrewsi
나는 이것을 다음과 같이 변경하려고했다 : 'while (mysqli_stmt_fetch ($ statement)) { return $ colUserID; }'그런 다음'$ response [ "userId"] = registerUser();를 통해 이것을 호출한다. 대신에 null을 리턴한다. – TheAnonymous010