Welcome Guest, Not a member yet? Register   Sign In
How to know if there is a record in the database using codeignaiter 3

Hi, I'm new to code-code and I wanted to know if they could help me with something I still do not understand, and it's like knowing if there is a record in the database and showing an alert saying that that record already exists in the database, please If someone could help me, it's a university project and I have two days to get it and I have 2 weeks trying to do that, please if someone can help me.

Depending on how advanced of a function you need you can use the built in function is_unique in form validation.

If you require more checks you need to write your own callback function and look for it yourself in the database:

Besides the option to create a rule as part of a form validation procedure, the CI query builder can be helpful:
PHP Code:
$number_of_records $this->db->count_all_results('table_name'); 
This will simply count all records in the table named 'table_name'.

And to check if a record with a specific value already exists:
(Function in a model):
PHP Code:
public function record_exists($conditions)
   $t $this->db->where($conditions)->count_all_results('table_name');
   return ($t 0);

To call this function from your controller, load the model first, then call the function:
PHP Code:
$record_exists $this->test_model->record_exists( array('first_name'=>'Paul') ); 

In this example, $record_exists will be true if the table 'table_name' holds one or more records that have the value 'Paul' in the 'first_name' field.

Theme © iAndrew 2016 - Forum software by © MyBB