[英]cakephp get data from db
我是cakephp的新手,如何在cakephp中编写以下sql查询
$sql = "SELECT * FROM `user` WHERE `email` = '" . $email . "' AND `password` = '" . $password . "'";
$result = mysql_query($sql, $connection);
while($fetchdata = mysql_fetch_assoc($result)){
echo $fetchdata['name'];
}
请帮我谢谢
请阅读此内容,以便使用cakephp框架轻松进行身份验证http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
在您的用户控制器中,您可以执行以下操作:
$this->set('user',$this->User->find('all', array ('conditions' => array('email' => $email, 'password' => $password))));
在您看来
foreach ($user as $us) {
echo($us['name']);
//your code
}
对于您的查询:
<?php
$this->loadmodel('User');
$result = $this->User->find('first',array('conditions' => array('email' => $email,
'password' => $password)));
foreach($result as $row)
{
//do whatever you want to do
}
?>
使用cakephp find
方法可以做到这一点。
<?php
$users = $this->User->find('first',array
(
'conditions' => array
(
'User.email' => $email,
'User.password' => $password
)
));
pr($users);
exit;
?>
我已经添加了pr($users);exit;
用于调试查询结果。
您还可以传递recursive
, limit
, order
等以及find方法, order
在Cakephp中阅读有关find
更多信息。
如果您在Users Controller中,则无需加载模型并连接数据库。 如果从另一个模型访问用户模型,则将用户模型加载到控制器中。 您可以通过两种方式加载模型
$this->loadmodel('User');
or
public $uses = array('User');
$results = $this->User->find('all',array('conditions' => array('email' => $email,'password' => $password)));
$this->set(compact('results '));
现在在您的视图文件中使用此
foreach ($results as $users) {
echo h($users['User']['email']);
echo h($users['User']['password']);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.