问题
In my mode I am selecting a field as
$query1 = $this->db->query("SELECT dPassword
FROM tbl_login
WHERE dEmailID='a@a.in'");
How to return dpassword
as a variable to my controller
I tried this way return dpassword;
回答1:
The following is also fine:
if($query1->num_rows() > 0){
$row = $query1->row();
}
return $row->dPassword;
Then if your query was to return more than a single row you could operate on the results like so:
foreach($query1->result() as $row){
echo $row->field1;
echo $row->field2;
echo $row->etcetera;
}
For single row results i usually return the row directly from the model like so:
return $query1->row();
Here is an example of this:
function select_provider_details($provider_id)
{
$this->db->select('*');
$this->db->from('providers');
$this->db->where('provider_id', $provider_id);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$result['success'] = TRUE;
$result['query'] = $query->row();
}
else
{
$result['success'] = FALSE;
$result['error'] = "Provider not found in database";
$result['errorcode'] = "E003";
$result['query'] = $query->row();
}
return $result;
}
Or for a query expected to return multiple results i return the entire results object:
return $query1;
回答2:
Check out the Query Results section of the CI manual. $query1 is set to a mysql resource from the query you executed. You then need to call additional functions to get the data
http://codeigniter.com/user_guide/database/results.html
$dataArray=$query1->result_array();
return $dataArray["dPassword"];
回答3:
Maybe you can use tray this.
$query1 = $this->db->query("SELECT dPassword
FROM tbl_login
WHERE dEmailID='a@a.in'");
if($query1){
// if you are working with objects
return $query1->result();
// if you are working with arrays try
return $query1->result_array();
}else{
return false;
}
来源:https://stackoverflow.com/questions/2948693/how-to-send-the-my-sql-result-to-controller-in-codeigniter-as-a-variable