我的控制器上有这些代码,用于获取用户在文本框中输入的值:
$username = $this->request->data['employee_account']['username'];
$password = $this->request->data['employee_account']['password'];
我希望使用cakePHP代码来实现这种查询:
"SELECT * FROM accounts WHERE username = '$username' AND password = '$password'";
我如何使用 find() 来做到这一点?
假设您有带有姓名帐户的模型
$login = $this->Account->find('first', array(
'conditions' => array(
'Account.email' => $username,
'Account.password' => $password
)
));
if ($login)
{//do something}
else
{
}
StuR 几乎做对了,他只是在转义和绑定参数方面走错了路..
$this->EmployeeAccount->find('all', array(
'conditions' => array(
'accounts.username = ?' => $username,
'accounts.password = ?' => $password
)
));
此外,我不禁认为您正在尝试用此登录某人? CakePHP 有一个内置的身份验证组件,这样就无需编写这样的
find()
。另外,由于您需要帮助在 Cake 中编写非常基本的 SQL 查询,因此我建议您阅读手册中的检索数据页面。
首先,我会过滤您的请求数据并提取 SQL 查询的变量:
extract(array_map('mysql_real_escape_string', $this->request->data['employee_account']));
那么这应该可以完成工作:
$this->accounts->find('all', array('conditions' => array('accounts.username' => $username, 'accounts.password' => $password)));