Welcome Guest, Not a member yet? Register   Sign In
How to know if there is a record in the database using codeignaiter 3
#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


Messages In This Thread
RE: How to know if there is a record in the database using codeignaiter 3 - by Wouter60 - 01-04-2019, 12:37 PM



Theme © iAndrew 2016 - Forum software by © MyBB