euel Posted January 19, 2012 Share Posted January 19, 2012 Gud pm!! So i made this script to check if a username and password exist but it kinda fails me. It gives me a database connection error from my confirm_query() which means it returned false? so here's my code: function check_if_exist($username, $password) { global $connection; $query = "SELECT username, hashed_password"; $query .= "FROM users "; $query .= "WHERE username = '$username' "; $query .= "AND hashed_password = '$password' "; $query .= " LIMIT 1"; $result_set = mysql_query($query, $connection); confirm_query($result_set); if(mysql_num_rows($result_set) == 1) { return true; } else { return false; } } my confirm_query(): function confirm_query($result) { if(!$result) { die("Database connection failure ". mysql_error()); } } I have a another script to check if the email exist and it works perfectly, i dunno why this won't work properly.. :-\ maybe another simple mistake.. Any ideas or Violent reaction? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/255340-check-if-exist/ Share on other sites More sharing options...
Proletarian Posted January 19, 2012 Share Posted January 19, 2012 I'm curious. Why are you using a global $connection variable? Why not just pass it as a parameter? Also, I suggest changing your confirm_query($) function to return a value, instead of dying, so you can handle the error better when it fails. Quote Link to comment https://forums.phpfreaks.com/topic/255340-check-if-exist/#findComment-1309148 Share on other sites More sharing options...
euel Posted January 19, 2012 Author Share Posted January 19, 2012 Because I'm referring to my db $connection which is located somewhere else.. since it does not change.. making it global makes it available if needed it while not repeating my self over and over again.. Anyways back to my problem, i think i do not need to that since it has mysql_error() already in it and it does report about an error on this line : $query .= "WHERE username = '$username' "; $query .= "AND hashed_password = '$password' "; $query .= " LIMIT 1"; the problem is i dunno what's wrong since like i have told from my 1st post that i have a same script for my email checking which works perfectly and it's this: function check_if_email_exist($email) { global $connection; $query = "SELECT email "; $query .= "FROM users "; $query .= "WHERE email = '$email' "; $query .= " LIMIT 1"; $result_set = mysql_query($query, $connection); confirm_query($result_set); if(mysql_num_rows($result_set) == 1) { return true; } else { return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/255340-check-if-exist/#findComment-1309151 Share on other sites More sharing options...
euel Posted January 21, 2012 Author Share Posted January 21, 2012 missing a stupid space in my query $query = "SELECT username, hashed_password"; should be $query = "SELECT username, hashed_password "; [code] Quote Link to comment https://forums.phpfreaks.com/topic/255340-check-if-exist/#findComment-1309749 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.