Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 302 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CodeIgniter - return only one row?

#1
At the moment if I am doing a query on the database that should only return one row, using:

...query stuff...
$query = $this->db->get();
$ret = $query->result();
return $ret[0]->campaign_id;

Is there a CodeIgniter function to return the first row?
something like `$query->row();`

Or even better would be the ability to, if there was only one row,
to just use the query object directly.

e.g. `$query->campaign_id;`
Reply

#2
To add on to what Alisson said you could check to see if a row is returned.

// Query stuff ...
$query = $this->db->get();

if ($query->num_rows() > 0)
{
$row = $query->row();
return $row->campaign_id;
}

return null; // or whatever value you want to return for no rows found
Reply

#3
This is better way as it gives you result in a single line:

$this->db->query("Your query")->row()->campaign_id;
Reply

#4
To make the code clear that you are intending to get the first row, CodeIgniter now allows you to use:

if ($query->num_rows() > 0) {
return $query->first_row();
}

To retrieve the first row.
Reply

#5
Change only in two line and you are getting actually what you want.

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

try it.
Reply

#6
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:

[To see links please register here]

Reply

#7
$this->db->get()->row()->campaign_id;
Reply

#8

class receipt_model extends CI_Model {

public function index(){

$this->db->select('*');

$this->db->from('donor_details');

$this->db->order_by('donor_id','desc');

$query=$this->db->get();

$row=$query->row();

return $row;
}
}
Reply

#9
**If you require to get only one record from database table using codeigniter query then you can do it using row(). we can easily return one row from database in codeigniter.**

$data = $this->db->get("items")->row();
Reply

#10
You can do like this
```
$q = $this->db->get()->row();

return $q->campaign_id;
```
Documentation :

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through