PDA

View Full Version : SQL function call shows up blank - where's the error?


lovethedog
06-18-2007, 10:27 PM
I am trying to create/call a function to check if a username exists during registration. I've created the following function (which I think is the problem, but can't be sure) in a file called functionsphp (which I put here so I could use the username exists check on other parts of the site):
function user_exists($username)
{
$result = $this->db->getVar("SELECT count(*) FROM accounts WHERE username = ?");
if ($result > 0) {
return true;
} else {
return false;
}
}

The idea here is to query the database for usernames, and if one is present, return a true. I'm trying to call it from a registration routine in registerphp, all of which is functioning properly except this one call:
$userexists = $functions->user_exists($username);
if ($userexists) {
$errors[] = "Sorry, that username is already taken. Please try again.";
}

When I execute this, I get a blank page back (from the smarty template).

Note - the register file does have a require_once declared for the function file, up top, and $username is the variable name for the username that is input on the form (and it is valid, as I've used it successfully for other tests in the registration process). Additional note - the reason I think the problem is the sql is because I actually tried running the function directly from the registration check script instead of calling it, and I get the same blank page.

What am I doing wrong here?

mr e
06-18-2007, 10:32 PM
I can't tell from your code if the functions are inside a class or not, they look like they are, but you didn't mention any classes, are they? And if they are, has that class been instantiated?

lovethedog
06-19-2007, 12:45 AM
It's in a class, and I've narrowed the errors down (getting rid of the parsing problems).

The function looks like this now:
function user_exists($username)
{
$result = $this->db->getCol("SELECT count(*) FROM accounts WHERE username = ?");
if ($result > 0) {
return true;
} else {
return false;
}
}

and is being called like so:
$userexists = $functions->user_exists($username);
if ($userexists) {
$errors[] = "Sorry, that username is already taken. Please try again.";
}

Now, it just rejects all usernames as existing - I think I have a logic problem.

Fumigator
06-19-2007, 04:19 AM
It depends entirely on what your class is doing (getCol function specifically).