This is my registration code using PHP and SQLite. It keeps going to the statement "user already exists" when it certainly does not. I check to see if the username is greater than one in the database (if it exists) and it does not, but if I echo out the count it always says its equal to one...
$checkUser = $db->prepare('
SELECT COUNT(*)
FROM users
WHERE
user = ?
');
$checkUser->setFetchMode(PDO::FETCH_ASSOC);
$checkUser->execute(array($user));
$numrows = count($checkUser);
if ($numrows == 0) {
try {
$reg = $db->prepare("
INSERT
INTO users
(user, pass, email)
VALUES
('$user', '$pass', '$email')
");
$reg->setFetchMode(PDO::FETCH_ASSOC);
$reg->execute();
} catch (PDOException $e) {
print_r($e->getMessage());
}
echo ('success...');
}
else {
die ('user already exists');
}
I don't use SQLite, but you look like your $numrows
are just checking that the execute actually ran successfully (boolean true/false
). Try updating this bit:
// I like to name "count" otherwise returned as "COUNT(*)" in the array key
$checkUser = $db->prepare('
SELECT COUNT(*) as count
FROM users
WHERE
user = ?
');
// Execute with bind
$checkUser->execute(array($user));
// Fetch an associative array
$result = $checkUser->fetch(PDO::FETCH_ASSOC);
// The count will be here as a numeric
if ($result['count'] == 0) {