I just took up a old project and the first thing I needed to do was to migrate from the mysql_*
extension to the mysqli_*
one. I haven't worked with PHP much before... Mosts of the new code works but in the examples below I seems to mess things up...
Old function:
function user_id_from_username($username) {
$username = sanitize($username);
return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0, 'user_id');
}
New(none working) function:
function user_id_from_username($username) {
$username = sanitize($username);
$id = mysqli_query(connect(),"SELECT `user_id` FROM `users` WHERE `username` = '$username'");
return $id;
}
Another old one:
function login($username, $password) {
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'"), 0) ==1) ? $user_id : FALSE;
}
And the new one:
function login($username, $password) {
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
$check = mysqli_query(connect(),"SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'");
return $check == $user_id ? TRUE : FALSE;
}
My sanitize Function:
function sanitize($data) {
return htmlentities(strip_tags(mysqli_real_escape_string(connect(), $data)));
}