Jump to content

[SOLVED] Question about returning values in functions


limitphp

Recommended Posts

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);

 

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);
}

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.

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);
}

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);
}

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.