我的控制器上有以下代码,用于获取用户在文本框中输入的值:
$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()执行此操作??
发布于 2012-05-31 18:25:26
假设您有名为accounts的模型
$login = $this->Account->find('first', array(
'conditions' => array(
'Account.email' => $username,
'Account.password' => $password
)
));
if ($login)
{//do something}
else
{
}发布于 2012-05-31 18:56:11
StuR几乎是正确的,他只是在转义和绑定参数方面走错了路。
$this->EmployeeAccount->find('all', array(
'conditions' => array(
'accounts.username = ?' => $username,
'accounts.password = ?' => $password
)
));此外,我不禁认为您正在尝试使用这个?CakePHP has a built in authentication component登录某人,它消除了编写这样的find()的需要。此外,由于在Cake中编写非常基本的SQL查询需要帮助,因此我建议您阅读手册中的Retrieving your data页面。
发布于 2012-05-31 18:20:39
首先,我会过滤您的请求数据并提取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)));https://stackoverflow.com/questions/10831467
复制相似问题