如果我将echo "h";
放在if语句中,我什么也得不到。此外,如果我在SQL代码中放置一个WHERE子句并使用该方法,我也不会在foreach循环中返回任何内容。关于为什么会发生这种情况的任何想法?我只需要与登录的当前用户匹配的用户。脚本正在做的是循环遍历users表中的所有行
public function dcr() {
// pass all dashboard accesses through this function
$username = $this->session->userdata("username");
$query = $this->db->query("SELECT * FROM users");
foreach ($query->result() as $row) {
echo "h";
if ($username == $row->username) {
$data = array('firstname' => $row->firstname);
$this->load->view('dashboard', $data);
}
}
}
答案 0 :(得分:1)
这应该是诀窍(尚未测试),你不需要if语句,只需使用函数get_where,因为代码将变得更清晰和可读。
public function dcr() {
// pass all dashboard accesses through this function
$username = $this->session->userdata("username");
$query = $this->db->get_where('users', array('username' => $username));
foreach ($query->result() as $row) {
$data = array('firstname' => $row->firstname);
$this->load->view('dashboard', $data);
}
}