views:

922

answers:

1

Hey guys. I'm re-creating a registration for my website using codeigniter. On my previous page I did it in simple php and HTML. Now that I'm trying to recreate it in codeigniter I seem to be running into difficulties.

The following code is a function that is called to validate an email address that the user puts in. It is supposed to query the database and check if the email already exists. If it does, print an error message, if not, everything is hunky dorey.

function email_check($str)
    {
       $num = $this->db->count_all("SELECT email FROM mytable WHERE email='$str'");
     if ($num > 0)
     {
      $this->validation->set_message('username_check', 'Email already in use');
      return FALSE;
     }
     else
     {
      return TRUE;
     }
    }

However I keep on getting mysql errors. Along the lines of

Error Number: 1064

You have an error in your SQL syntax;

or

alternatively when i use

function email_check($str)
    {
      $database_email_check = ("SELECT email FROM myTable WHERE email='$str'");
      $email_check = $this->db->query($database_email_check);
      $num = mysql_num_rows($email_check);
     if ($num > 0)
     {

      $this->validation->set_message('username_check', 'Email already in use');
      return FALSE;
     }
     else
     {
      return TRUE;
     }
    }
}

gives the error

mysql_num_rows(): supplied argument is not a valid MySQL result resource

Any help would be appreciated. Including a facepalm and correction in my code. I'm convinced it's something stupid that I just can't see.

Thanks

+2  A: 

The function you're looking for is count_all_results, and you are only supposed to put the table name as your parameter for count_all_results.

$this->db->where('email', $str);
$num = $this->db->count_all_results('mytable');

See CodeIgniter's Active Record page for more info

jimyi
Thank you so much.... I've been banging by head against the wall for an hour and a half :P
Chris Salij