limitphp Posted December 4, 2008 Share Posted December 4, 2008 I'm still a little wet behind the ears with functions. I want to create a function that checks to see if a username is taken: function does_username_exist($username) { $queryUsername = "SELECT username FROM user WHERE username = '$username'"; $result = mysql_query($queryUsername) or die (mysql_error()); $rowCount=mysql_affected_rows(); if ($rowCount>0) { $usernameTaken = 1; }else { $usernameTaken = 0; } return $usernameTaken ; } Is that the correct way to do it? Do you have to return the value you inputted? I input $username into the function, but I'm returning a different variable. Is that ok? Can you return multiple variables? Is this the proper way to use the function?: $usernameTaken = does_username_exist($username); Quote Link to comment https://forums.phpfreaks.com/topic/135557-solved-question-about-returning-values-in-functions/ Share on other sites More sharing options...
.josh Posted December 4, 2008 Share Posted December 4, 2008 everything looks fine, except that mysql_affected_rows is only used for things that alter the table (insert, update, delete, etc...) You'll want to use mysql_num_rows instead Quote Link to comment https://forums.phpfreaks.com/topic/135557-solved-question-about-returning-values-in-functions/#findComment-706175 Share on other sites More sharing options...
rhodesa Posted December 4, 2008 Share Posted December 4, 2008 yeah, mysql_num_rows()...and also, don't bother with an extra variable...just return the 1 or 0 as soon as you know: function does_username_exist($username) { $queryUsername = "SELECT username FROM user WHERE username = '$username'"; $result = mysql_query($queryUsername) or die (mysql_error()); $rowCount=mysql_num_rows($result); if ($rowCount>0) { return 1; } return 0; } edit: for that matter...just force mysql_num_rows to a boolean: function does_username_exist($username) { $queryUsername = "SELECT username FROM user WHERE username = '$username'"; $result = mysql_query($queryUsername) or die (mysql_error()); return (boolean) mysql_num_rows($result); } Quote Link to comment https://forums.phpfreaks.com/topic/135557-solved-question-about-returning-values-in-functions/#findComment-706181 Share on other sites More sharing options...
.josh Posted December 4, 2008 Share Posted December 4, 2008 Do you have to return the value you inputted? No. You don't have to return anything at all. I input $username into the function, but I'm returning a different variable. Is that ok? Yes. Can you return multiple variables? No, not individually. But you can return an array. Quote Link to comment https://forums.phpfreaks.com/topic/135557-solved-question-about-returning-values-in-functions/#findComment-706182 Share on other sites More sharing options...
limitphp Posted December 4, 2008 Author Share Posted December 4, 2008 Awesome, thanks for the info guys! this looks great and more simplified: function does_username_exist($username) { $queryUsername = "SELECT username FROM user WHERE username = '$username'"; $result = mysql_query($queryUsername) or die (mysql_error()); return (boolean) mysql_num_rows($result); } Quote Link to comment https://forums.phpfreaks.com/topic/135557-solved-question-about-returning-values-in-functions/#findComment-706185 Share on other sites More sharing options...
rhodesa Posted December 4, 2008 Share Posted December 4, 2008 Awesome, thanks for the info guys! this looks great and more simplified: function does_username_exist($username) { $queryUsername = "SELECT username FROM user WHERE username = '$username'"; $result = mysql_query($queryUsername) or die (mysql_error()); return (boolean) mysql_num_rows($result); } actually...one more edit...to protect from SQL Injection: function does_username_exist($username) { $queryUsername = sprintf("SELECT username FROM user WHERE username = '%s'",mysql_real_escape_string($username)); $result = mysql_query($queryUsername) or die (mysql_error()); return (boolean) mysql_num_rows($result); } Quote Link to comment https://forums.phpfreaks.com/topic/135557-solved-question-about-returning-values-in-functions/#findComment-706189 Share on other sites More sharing options...
limitphp Posted December 4, 2008 Author Share Posted December 4, 2008 Awesome, thanks for the info guys! this looks great and more simplified: function does_username_exist($username) { $queryUsername = "SELECT username FROM user WHERE username = '$username'"; $result = mysql_query($queryUsername) or die (mysql_error()); return (boolean) mysql_num_rows($result); } actually...one more edit...to protect from SQL Injection: function does_username_exist($username) { $queryUsername = sprintf("SELECT username FROM user WHERE username = '%s'",mysql_real_escape_string($username)); $result = mysql_query($queryUsername) or die (mysql_error()); return (boolean) mysql_num_rows($result); } Actually my $username already goes through: $username = check_input($_POST['username']); function check_input($value) { // Stripslashes if (get_magic_quotes_gpc()) { $value = stripslashes($value); } $value = mysql_real_escape_string($value); return $value; } Which I learned from you guys as well. Quote Link to comment https://forums.phpfreaks.com/topic/135557-solved-question-about-returning-values-in-functions/#findComment-706191 Share on other sites More sharing options...
rhodesa Posted December 4, 2008 Share Posted December 4, 2008 awesome Quote Link to comment https://forums.phpfreaks.com/topic/135557-solved-question-about-returning-values-in-functions/#findComment-706193 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.