php - 无法显示 SELECT 结果 sql
问题描述
我尝试获取 gmail = $gmail 的 id 并将其用于 UPDATE 表
但我不能从 id 使用并显示错误 Undefined property: 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;
}
}
解决方案
You have to use curly braces when calling a method in an object:
$sqlCommand = "UPDATE users
SET verificationcode = '$random'
WHERE id = {$result->fetch_assoc()}";
An alternative is using concatenation:
$sqlCommand = "UPDATE users
SET verificationcode = '$random'
WHERE id = " . $result->fetch_assoc();
Note that in this case you could combine the two SQL statements into just one, for example update .. where id = (select id from ...)
.
Also note that your code as posted is vulnerable to an SQL injection attack. See How can I prevent SQL injection in PHP?
推荐阅读
- powershell - X509Linux 系统上 .NetCore 的存储位置
- c# - 启用 CORS,但拒绝通过浏览器直接访问
- nativescript - 如何导航回 Nativescript-vue 中的特定组件或 backstack 条目?
- php - Guzzle Pool:如何等待所有请求完成而不超时?
- nginx - 流式传输实时视频和仅将音频中继到 icecast2 服务器
- javascript - 谷歌地球引擎:点值提取错误
- hive - 无法获取表 hive_table
- kubernetes-helm - 具有多个端口的 Helm 模板 kubernetes 服务
- python - 数据无法使用python在scrapy中检索多个类名
- node.js - 使用 Angular 从 Express 中捕获全局错误