...

View Full Version : Count() not properly returning count of row



justin7410
04-11-2013, 11:14 PM
Hey guys , so i had a issue on the php forums regarding my register/login system i was creating.

the link to the post : http://www.codingforums.com/showthread.php?t=291851

anyway, i figured out that my problem is more a problem with my sql not querying correctly , as it is my code not functioning right.

so i have a function


function email_exists($email) {
$email = sanitize($email);
$query = (" SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email' ");
## Oops, you were using $email which is the UN-sanitized version of that value

# Let's see what the query looks like
printf("Query: %s<BR>", $query);

$results = mysql_query($query);

// Let's see if the query succeeded
if (!$results) {
printf("Query Failed!<BR>%s<BR>", mysql_error());
/* We probably should do something more intelligent than just return false, but
I don't know the application so, hey, I didn't find the email address. */
return false;
}

// OK, The query executed, let's see what we found
if (mysql_num_rows($results) == 0) {
/* This should never happen. Since we used COUNT(user_id), we should at least
get a row with a count of zero. */
printf("Query Returns Zero Rows???<BR>");
/* We probably should do something more intelligent than just return false, but
I don't know the application so, hey, I didn't find the email address. */
return false;
}

// OK, Now let's see what we got
$theCount = mysql_result($results, 0);
# Show the count, just so we know
printf("Query Says Count is %d<BR>", $theCount);

return ($theCount > 0);
}



and when you go the browser the following is outputted:


Query: SELECT COUNT(`user_id`) FROM `users` WHERE `email` = 'justin7410@email.com'
Query Says Count is 0
Array ( ) // this empty array is an $errors array that come up empty due to no errors returning.

Now anytime a user registers an email address, regardless of it existing already, it returns a bool of false.

so i ask this now in this forum, because i cant understand why its returning 0 even when the query should be right.

when i echo the query with my variables from the $_POST you get the right sanitized email as show above.

im just not understanding why this is not returning a count from the Database.

any suggestions ?

i have been stuck on this issue for days now , so any help is completely appreciated.

knightCoder
04-12-2013, 04:21 AM
make sure your column names in the db table match the column names in your sql query.

Change the user entered email address and email address in the db to lowercase or uppercase to make a valid comparison because when comparing 2 strings


justin7410@email.com
is no the same as Justin7410@email.com



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum