Reputation: 31
i try to get id that gmail = $gmail and use it for UPDATE table
but i can't use from id and showin error Undefined property: mysqli_result::$fetch_assoc
function 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;
}
}
Upvotes: 1
Views: 27
Reputation: 243
fetch_assoc gives you the array of the record :) try this code
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";
}
}
now it will work. good luck :)
Upvotes: 0
Reputation: 111329
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?
Upvotes: 1