• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to know if there is a record in the database using codeignaiter 3

#1
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.
Reply

#2
Depending on how advanced of a function you need you can use the built in function is_unique in form validation.
https://www.codeigniter.com/userguide3/l...ding-rules

If you require more checks you need to write your own callback function and look for it yourself in the database:
https://www.codeigniter.com/userguide3/l...-as-a-rule
Reply

#3
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:
$this->load->model('test_model');
$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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.