php 从数据库中获取单个值 - Codeigniter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16499947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a single value from database - Codeigniter
提问by wdphd
I'm trying to get first name and last name of registered users. This is my getfirstname , getlastname function in the model
我正在尝试获取注册用户的名字和姓氏。这是我在模型中的 getfirstname , getlastname 函数
public function getf()
{
$email = $this->input->post('email');
$this->db->where('email', $this->input->post('email'));
$query = $this->db->get('users');
if($query->num_rows == 1)
{
$row = $query->row();
echo $row->fname;
}
}
public function getl()
{
$this->db->where('email', $this->input->post('email'));
$query = $this->db->get('users');
if($query->num_rows == 1)
{
$row = $query->row();
echo $row->lname;
}
}
In My Contoller:
在我的控制器中:
public function members()
{
if($this->session->userdata('is_logged_in'))
{
$data['fname'] = $this->getf();
$data['lname'] = $this->getl();
$this->load->view('members', $data);
}
else
{
redirect('main/restricted');
}
}
Im echoing the fname and lname variables in my view which prints 'Array' and not actual firstname and lastname
我在我的视图中回显 fname 和 lname 变量,它打印“数组”而不是实际的名字和姓氏
echo $fname;
echo $lname;
回答by Muhammad Raheel
Your call to model is totally wrong and you are not following the standard procedure. Try it like this. Seperate the logics
您对模型的调用是完全错误的,并且您没有遵循标准程序。像这样试试。分离逻辑
Controller
控制器
public function members()
{
if($this->session->userdata('is_logged_in'))
{
$email = $this->input->post('email');
$result = $this->mymodel->getnames($email);
if($result){
$data['fname'] = $result->first_name;
$data['lname'] = $result->last_name;
$this->load->view('members', $data);
}
}
else
{
redirect('main/restricted');
}
}
And create single function for model not two functions for two values that could be retrieved with single function Also return the object instead of echoing
并为模型创建单个函数,而不是为可以使用单个函数检索的两个值的两个函数同时返回对象而不是回显
public function getnames($email)
{
$this->db->where('email',$email);
$query = $this->db->get('users');
if($query->num_rows == 1)
{
return $query->row();
}
return false;
}
回答by lysenkobv
UserModel
用户模型
class UserModel extends CI_Model
{
public function getItemByEmail($email)
{
// check email
$this->load->helper('email');
if( ! $email OR ! valid_email($email))
return array();
$this->db->where('email', $email);
$this->db->limit(1);
$item = $this->db->get('users')->row();
return $item;
}
}
Controller:
控制器:
public function members()
{
if( ! $this->session->userdata('is_logged_in'))
redirect('main/restricted');
$this->load->model('UserModel');
$this->load->view('members', array(
'userRow' => $this->UserModel->getItemByEmail($this->input->post('email', TRUE))
));
}
View:
看法:
<?= (isset($userRow->fname) ? $userRow->fname : ''); ?>
回答by Daniel Morgan
From what I can see you haven't actually declared the 'fname' and 'lname' variables, unless I've missed it, but if it keeps returning 'Array' then it is because you need to serialise the array with
从我所看到的,你实际上并没有声明 'fname' 和 'lname' 变量,除非我错过了它,但如果它一直返回 'Array' 那么那是因为你需要序列化数组
$variable = json_encode($arrayName);
This will turn the array into a string, hope this helps.
这会将数组转换为字符串,希望这会有所帮助。