Saturday, 17 August 2013

Mysql to Mysqli conversion issue: login

Mysql to Mysqli conversion issue: login

I had a simple login script that worked perfectly fine under mysql and im
currently trying to port it to mysqli and i just cant get it to work.
login($username, $password) {
$link = mysqli_connect('localhost', 'user', 'pw', 'db');
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
$loginquery = "SELECT COUNT(DISTINCT user_id) FROM users WHERE
username='$username' AND password = '$password'";
$count = mysqli_query($link, $loginqueryquery);
$countarray = mysqli_fetch_array($count, MYSQLI_BOTH);
$countarrayres = $countarray[0];
if($countarrayres == 1) {return true ;} else {return false ;}
}
ive tried it all on a test page: i set $username = test, and $password =
test, which tallies with my "test" user in my db. this produces a
$countarrayres = 1 when i print it. i also get a print of 0 for the same
if i change either the pw or username to something non-existant so its
suggests it works, but im baffled.
the page this script acts in is simply:
$test = login($username, $password);
if( $login === false ) { "wrong password/username bro"; } else {
//log in based on session user_id;}
ive tried changing false to true but i continue to get the "wrong
password/username bro" message so it would suggest theres a bigger problem
than the function perhaps? im miffed as to whats going on here, because
the only thing thats changed is the mysql -> mysqli functions, nothing
else. any ideas?

No comments:

Post a Comment