To print the exact SQL query in CodeIgniter and troubleshoot why it’s failing, you can follow these steps:
Use $this->db->last_query(): This function shows the last executed SQL query, including the values from any bound parameters
Update Your Model Code: Modify your code to output the SQL query if it fails, as shown below:
$query = $this->db->query($sql, array(fields, fields1));
if ($query) {
return true;
} else {
// Display the SQL query that was executed
echo "Failed. The query was: " . $this->db->last_query();
return false;
}
Enable Debugging for More Details:
Set 'db_debug' => true in application/config/database.php. This will show detailed database errors directly in the browser, helping with troubleshooting.