Problem overview
I have a function that updates a user's username in my SQL database. I am trying to add some error handling (like a return true/false) but I don't know how to go about doing that.
The current function
function updateUid($conn, $usersUid, $rowId) {
$sql = "UPDATE users SET usersUid = ? WHERE usersId = ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
//error - bad SQL call
exit();
}
mysqli_stmt_bind_param($stmt, "ss", $usersUid, $rowId);
mysqli_stmt_execute($stmt);
return true;
mysqli_stmt_close($stmt);
}
As you can see, right now it's just returning true without regard for weather or not the function worked.
What I've tried
I tried using mysqli_stmt_get_result($stmt)
but I don't think I'm using it correctly:
function updateUid($conn, $usersUid, $rowId) {
$sql = "UPDATE users SET usersUid = ? WHERE usersId = ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
//error - bad SQL call
exit();
}
mysqli_stmt_bind_param($stmt, "ss", $usersUid, $rowId);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($resultData)) {
return $row;
} else {
$result = false;
return $result;
}
mysqli_stmt_close($stmt);
}
As far as I can tell, mysqli_stmt_get_result()
is returning nothing, which makes sense since I'm not fetching data, but rather placing it.
Desired outcome
As previously stated, I would really like to be able to get a true/false return as to whether the function worked or not. What is the best method for testing the result of a SQL injection in PHP?