How to pass variable value to sql query in codeigniter?

by herminia_bruen , in category: PHP Frameworks , 8 months ago

How to pass variable value to sql query in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cali_green , 8 months ago

@herminia_bruen 

To pass a variable value to a SQL query in CodeIgniter, you can use the Query Builder class provided by CodeIgniter. Here's an example of how to do it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$variable = 'some value';

// Build the SQL query using the Query Builder class
$this->db->select('*');
$this->db->from('table_name');
$this->db->where('column_name', $variable);

// Execute the query
$query = $this->db->get();

// Fetch the results
$results = $query->result();


In this example, we are passing the value of the $variable variable to the where() method of the Query Builder class. This will replace column_name in the SQL query with the value of the variable.