user2702406 user2702406 - 2 months ago 93x
PHP Question

how to check if username already exists in codeigniter

I am working on a site in codeigniter.I am not so expert using framework.Hera I have to check if email already exists in database.I have coded the required functionality but I am getting an error when submit the form.
My code is:
In controller i have made the following rule.

'email', 'Email', 'trim|required|valid_email|is_unique|callback_isEmailExist'
public function isEmailExist($email) {
$is_exist = $this->user->isEmailExist($email);

if ($is_exist) {
'isEmailExist', 'Email address is already exist.'
return false;
} else {
return true;

in model user the function is :

function isEmailExist($email) {
$this->db->where('email', $email);
$query = $this->db->get('users');

if ($query->num_rows() > 0) {
return true;
} else {
return false;

When I submit the form, I am getting the following errors.

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: libraries/Form_validation.php

Line Number: 953

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ` = '' LIMIT 1' at line 2


Filename: C:\xampp\htdocs\aunction\system\database\DB_driver.php

Line Number: 330
Anyone help me please.
Thanks in advance.


Your problem is in the set_rules line. Here you use both is_unique and a callback function. You have to use anyone of that. If you use a call_back function to check duplicate data; doesn't need to use is_unique. For this wrong you get that error Just remove the is_unique from there.

$this->form_validation->set_rules('email','Email','trim|required|valid_email|callback_isEmailExist'); // removed is_unique

Try this out and let me know.