I'm trying to figure out the difference between using prepared statements and and escaping/converting a variable into a string as follows:
$sql = "SELECT `user_id`, `user_name` FROM table WHERE `user_id` = ? and `user_name`= ?";
$sqlPrepare = $conn->prepare($sql);
$sqlPrepare->bind_param('ss', $user_id, $user_name);
$sqlPrepare->execute();
$result = $ $sqlPrepare->get_result();
if($result->num_rows ===0)
{
// Do work
}
VS
mysqli::real_escape_string($whatever_vars_needed);
$sql = "SELECT `user_id`, `user_name` FROM table WHERE `user_id` = '".$user_id."' and `user_name`= '".$user_name."'";
$sqlQuery = $conn->query($sql);
if($sqlQuery->num_rows ===0)
{
// Do work
}
as far as protecting against sql injections go, would they both serve the same purpose? And if so, wouldn't it be preferred to use the second method since it does save a bit of extra typing?
I realize that I'm using query vs prepare but even then I don't really see the difference if I'm converting the variables into strings?
Which is a better method?