我尝试获取ID = gmail = $ gmail并将其用于UPDATE表
但是我无法使用id和showin错误未定义的属性:mysqli_result :: $ fetch_assoc
函数addverificationcode($ gmail,$ random) {
$connection = mysqli_connect(DataBaseManager::HOST, DataBaseManager::USER, DataBaseManager::PASSWORD, DataBaseManager::DATABASENAME);
mysqli_set_charset($connection, "utf8");
$sqlQuery = "SELECT id FROM users WHERE gmail='$gmail'";
$result = mysqli_query($connection, $sqlQuery);
if ($result->num_rows > 0) {
$sqlCommand = "UPDATE users
SET verificationcode = '$random'
WHERE id = $result->fetch_assoc()";
}
if(mysqli_query($connection, $sqlCommand)){
return true;
}else{
echo("Error description: " . mysqli_error($connection));
return false;
}
}
答案 0 :(得分:1)
在对象中调用方法时必须使用curly braces:
$sqlCommand = "UPDATE users
SET verificationcode = '$random'
WHERE id = {$result->fetch_assoc()}";
一种替代方法是使用串联:
$sqlCommand = "UPDATE users
SET verificationcode = '$random'
WHERE id = " . $result->fetch_assoc();
请注意,在这种情况下,您可以将两个SQL语句合并为一个,例如update .. where id = (select id from ...)
。
还请注意,您发布的代码容易受到SQL注入攻击的攻击。参见How can I prevent SQL injection in PHP?
答案 1 :(得分:0)
fetch_assoc为您提供记录的数组:) 试试这个代码
if ($result->num_rows > 0) {
while($res = mysqli_fetch_array($result)){
$id = $res['id']; // you are breaking the array here and storing the array index in a new variable
$sqlCommand = "UPDATE users
SET verificationcode = '$random'
WHERE id = $id";
}
}
现在它将可以使用。祝你好运:)