Re: Returning a variable from a function problem
| From: | Nicole | Date: | Wed, 02 Apr 2003 08:45:32 +0000 |
| Subject: | Re: Returning a variable from a function problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-141942@lists.php.net to get a copy of this message | ||
Hello,
Where is $duplicate originally being set? Are you using global variables? is
$duplicate being POST'd or GET'd or what?
If it is only gaining it's value from the duplicate_name() function, is that
function returning those possible values (1,2) based on whatever the result
of the action taken in that fuction is?
Maybe try this:
if (empty($form["name"])) {
$error["name"] = "*"; $message["name"] = "Required field!";
}
else
{
$duplicate = duplicate_name($form["name"]);
if ( $duplicate == 0 ) {
$error["name"] = "*"; $message["name"] = "Screen name already
used!";
}
else if ($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
tryagain
in a few moments!";
}
else {
// not a dup, do whatever
}
}
Nicole
@rogers.com> wrote in message news:001701c2f8e6$d994cf50$6401a8c0@p1...
> 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);
> }
>
>