I am learning to use prepared statements to add security against SQL injections... I have written the below code and am looking for feedback on if the code is correct/secure.
$user= mysqli_real_escape_string($conn, $_GET['user']);
$sql = "SELECT user_name, about, avatar FROM users WHERE user_name = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $user); // 's' specifies type => 'string'
$stmt->execute();
$result = $stmt->get_result();
while ($users = $result->fetch_assoc()) {
// Do something with the result
$avatar = !empty($users['avatar']) ? $users['avatar'] : 'default.png';
$uname = $users['user_name'];
$about = $users['about'];
}
the above code works fine, but if I made a mistake or forgot to add something please let me know as I am still learning about PHP/SQL security.