Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2013
    Thanked 0 Times in 0 Posts

    Count() not properly returning count of row

    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

    PHP Code:
    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($results0);
    # 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.

  • #2
    Join Date
    Mar 2013
    Thanked 9 Times in 9 Posts
    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

    is no the same as Justin7410@email.com


    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts