Codeigniter $This->Db->Get(), How to Return Values for a Specific Row

Codeigniter $this-db-get(), how do I return values for a specific row?

SOLUTION ONE

$this->db->where('id', '3');
// here we select every column of the table
$q = $this->db->get('my_users_table');
$data = $q->result_array();

echo($data[0]['age']);

SOLUTION TWO

// here we select just the age column
$this->db->select('age');
$this->db->where('id', '3');
$q = $this->db->get('my_users_table');
$data = $q->result_array();

echo($data[0]['age']);

SOLUTION THREE

$this->db->select('age');
$this->db->where('id', '3');
$q = $this->db->get('my_users_table');
// if id is unique, we want to return just one row
$data = array_shift($q->result_array());

echo($data['age']);

SOLUTION FOUR (NO ACTIVE RECORD)

$q = $this->db->query('SELECT age FROM my_users_table WHERE id = ?',array(3));
$data = array_shift($q->result_array());
echo($data['age']);

CodeIgniter - return only one row?

You've just answered your own question :)
You can do something like this:

$query = $this->db->get();
$ret = $query->row();
return $ret->campaign_id;

You can read more about it here: http://www.codeigniter.com/user_guide/database/results.html

Codeigniter $this-db-get(), how do I return values for a specific row?

SOLUTION ONE

$this->db->where('id', '3');
// here we select every column of the table
$q = $this->db->get('my_users_table');
$data = $q->result_array();

echo($data[0]['age']);

SOLUTION TWO

// here we select just the age column
$this->db->select('age');
$this->db->where('id', '3');
$q = $this->db->get('my_users_table');
$data = $q->result_array();

echo($data[0]['age']);

SOLUTION THREE

$this->db->select('age');
$this->db->where('id', '3');
$q = $this->db->get('my_users_table');
// if id is unique, we want to return just one row
$data = array_shift($q->result_array());

echo($data['age']);

SOLUTION FOUR (NO ACTIVE RECORD)

$q = $this->db->query('SELECT age FROM my_users_table WHERE id = ?',array(3));
$data = array_shift($q->result_array());
echo($data['age']);

get_where codeigniter return value of single column in table

You can use select query instead of plain get query, like so -

$kodemk = $this->db->select('kodemk')->from('tik.matakuliah')->where(array('namamk'=>$data['namamk'])->get()->result();
// returns select kodemk from tik.matakuliah where namamk = $data['namamk']

See if it helps you.

Get a single value from a result set returned from PHP codeigniter model

Try this. As you need single row based on username, you have to use row()(returns Object) or row_array() (returns array) instead of result() because result() or result_array() returns result set

public function getUserUid($username)
{
$this->db->select("*");
$this->db->from('sysuser');
$this->db->where('username',$username);
$query = $this->db->get();
$row = $query->row();
return $row->uid;
}

Codeigniter SQL query return single value

Below code is works fine for me.

$MAXID = 0;
$row = $this->db->query('SELECT MAX(id) AS answerid FROM pa_it_answer')->row();
if ($row) {
$MAXID = $row->answerid;
}

Codeigniter query doesn't return my selected rows

If you're using Codeigniter 3:

$query = $this->db->select('column1,column2')->from('table')->get();
print_r($query->result());

Ref: https://codeigniter.com/userguide3/database/query_builder.html


If you're using Codeigniter 4:

We'll need to use $query->getResult() instead

$query = $builder->get();

foreach ($query->getResult() as $row)
{
echo $row->title;
}

Ref: https://codeigniter.com/user_guide/database/query_builder.html

Return only single record on codeigniter

Use $this->db->limit(1); to get 1 record only.

CodeIgniter : Get value from single column from table

There are two ways to do that, you can choose which one you feel ease

Model:

public function gender_count()
{
$response = array();
//for female
$this->db->select('count(id) as ftotal');//id should be primary key
$this->db->from('personaldata');
$this->db->where('gender','f');
$this->db->group_by('gender');
$query = $this->db->get();
if ($query->num_rows() > 0) {
$response['f'] = $query->row('ftotal');
}else{
$response['f'] = 0;
}
//for male
$this->db->select('count(id) as mtotal');//id should be primary key
$this->db->from('personaldata');
$this->db->where('gender','m');
$this->db->group_by('gender');
$query = $this->db->get();
if ($query->num_rows() > 0) {
$response['m'] = $query->row('mtotal');
}else{
$response['m'] = 0;
}
return $response;//assosiative array
}

OR

public function gender_count()
{
$response = array();
$this->db->select('gender,count(id) as total');//id should be primary key
$this->db->from('personaldata');
$this->db->group_by('gender');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach($query->result() as $row){
$response[$row->gender] = $row->total;
}
}else{
$response['f'] = 0;
$response['m'] = 0;
}
return $response;//assosiative array
}

Controller:

$data = array();
$genderCount = $this->Gender_model->gender_count();
$data['fcount'] = $genderCount['f'];
$data['mcount'] = $genderCount['m'];
$this->load->view('genderCount',$data);

View:

<span>Female:</span><b><?php echo $fcount;?></b>
<span>Male:</span><b><?php echo $mcount;?></b>

How to retrieve the column values in codeigniter?

Do something like this :

$this->db->where('config_name', 'Account_activation');
$q = $this->db->get('my_users_table');
/* if u r fetching one row use row_array instead of result_array*/
$row = $q->row_array();

echo $row['config_value'];

For more : https://www.codeigniter.com/user_guide/database/index.html



Related Topics



Leave a reply



Submit