I'm new to CodeIgniter, I've tried to read the documentation of CI but I still can't solve my problem, maybe someone here can help fix my problem. Here is my code:
In my controller
class Registration extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('registration_model','rmod');
}
function ambil() {
$gender = $this->input->post('kelamin');
$tinggi = $this->input->post('height');
$berat = $this->input->post('weight');
$weight = $this->rmod->ambilBeratPria($tinggi);
echo $weight;
}
In my model
function ambilBeratPria($tinggi) {
$this->db->select('berat')->from('pria')->where('tinggi',$tinggi);
$query = $this->db->get();
return $query;
}
I want to get the result of my query in the model, but i get an error like this:
Message: Object of class CI_DB_mysql_result could not be converted to string
Maybe someone here can help to solve my problem ?
Thanks.
解决方案
You need to return the result of the query:
function ambilBeratPria($tinggi) {
$this->db->select('berat')->from('pria')->where('tinggi',$tinggi);
$query = $this->db->get();
return $query->result();
}
EDIT:
If the result is a single row:
function ambilBeratPria($tinggi) {
$this->db->select('berat')->from('pria')->where('tinggi',$tinggi);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row()->berat;
}
return false;
}