gpt4 book ai didi

php - mysqli_stmt_fetch() 不工作

转载 作者:行者123 更新时间:2023-11-30 22:16:43 26 4
gpt4 key购买 nike

我正在尝试从 mysqli_stmt_fetch() 语句中保存一个值。当我的应用程序运行时,它返回此变量的无值。我是 PHP 新手,无法完全调试此文件。错误在哪里?

我的 php 文件:

<?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);
?>

我要设置的变量位于 registerUser 函数内。它指出:

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);
?>

虽然它返回 null 作为“userId”而不是 ID...请注意 SQL 数据库中的 ID 不是 null。在我的测试用例中,ID 是 8。

用于从 Android 调用此 PHP 文件的 StringRequest 代码:

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;
}
}

最佳答案

您好,如果您需要last insert userID,您可以试试这个。那么这将对您有所帮助,我已经更改了您的一些代码

<?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;

$passwordHash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $connect->prepare("INSERT INTO user (name, username, theme, email, default_radius, password) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssss", $name, $username, $theme, $email, $defaultRadius, $passwordHash);
$stmt->execute();
return $userID = $stmt->insert_id;
}

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["success"] = true;
$response["userId"] = $userID;
} else {
$response["reason"] = 1;
}

echo json_encode($response);
?>

关于php - mysqli_stmt_fetch() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38024318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com