Returning a variable from a function problem
| From: | ODCS | Date: | Wed, 02 Apr 2003 07:09:46 +0000 |
| Subject: | Returning a variable from a function problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-141935@lists.php.net to get a copy of this message | ||
I have a form where the user inputs information - the code below is the
error checking for one of the fields. The first IF statement just checks
that the filed is not empty and works fine.
Then I check a function I made to see if the user is already in a mysql
database and the function returns 0, 1, or 2 depending on the various
conditions. The first elseif works as I would expect - but it seems that the
next two elseif's are just being ignored.
Could someone possibly explain what I have wrong here. Thanks
if (empty($form["name"])) { $error["name"] = "*";
$message["name"] =
"Required field!";
}
elseif($duplicate = duplicate_name($form["name"]) == 0) {
$error["name"] = "*"; $message["name"] = "Screen name
already used!";
.
}
elseif ($duplicate == 1) {
$error["name"] = "*"; $message["name"] = "Database error -
Please try
again in a few moments!";
}
elseif ($duplicate == 2) {
$error["name"] = "*"; $message["name"] = "Server error -
Please try
again in a few moments!";
}
function duplicate_name($name) {
if (!($connect = @mysql_connect($serverhost,$serveruser,$serverpass))) {
**** These are all defined above but not included here.
$server_error = "2";
}
if (!@mysql_select_db($databasename)) {
$server_error = "1";
}
if($server_error) { $duplicate = $server_error;
}
else
{
$query = "SELECT name FROM $tablename WHERE name = '$name'";
if(!$query) { $duplicate = 1}
else {
$result = mysql_query($query);
$line = mysql_fetch_array($result);
if($line['name'] == $name) { $duplicate = 0; }
mysql_free_result($result);
mysql_close($connect);
}
}
echo $duplicate; ***** This shows that the variable $duplicate is
assigned 0, 1, or 2 depending on the what conditions were met.
return($duplicate);
}