I use CodeIgniter for my project and this is my code in model
public function group_all_ville(){
$this->db->select('*');
$this->db->from('departement');
$this->db->join('villes', 'villes.num_dept = departement.num_dept');
$this->db->group_by('nom_dept');
$query = $this->db->get();
return $query->result();
}
and this is error after execution
A Database Error Occurred
Error Number: 1055
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'pneu.departement.id_dept' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT * FROM `departement` JOIN `villes` ON `villes`.`num_dept` = `departement`.`num_dept` GROUP BY `nom_dept`
Filename: C:/wamp64/www/pneu/system/database/DB_driver.php
Line Number: 691
I try different method but nothing result. Thanks
There is a db->close() method. In a PHP environment that is not using persistent connections there is no real need to call it. For MySQL in particular, all open non-persistent MySQL connections and result sets are automatically destroyed when a PHP script finishes its execution.
We can get last executed query using last_query() function of db class in codeigniter. It is a very simple to use $this->db->last_query() function to see SQL statements of last executed query in php codeigniter app.
CodeIgniter gives you access to a Query Builder class. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action.
use order_by
clause as well
$this->db->group_by('nom_dept');
$this->db->order_by('nom_dept', 'asc'); # or desc
FYI : Setting SQL mode and Session set is not fix the actual error.
Examples (Better not to Do)
This solution worked for me:
$this->db->select('d.nom_dept');
$this->db->from('departement AS d, villes as v');
$this->db->where('v.num_dept = d.num_dept');
$this->db->group_by('d.nom_dept');
Refer to 12.20.3 MySQL Handling of GROUP BY for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With