[英]cakephp get data from db
I'm new to cakephp how to write below sql query in cakephp 我是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'];
}
Please help me Thanks 请帮我谢谢
请阅读此内容,以便使用cakephp框架轻松进行身份验证http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
In your User controller you can do something like: 在您的用户控制器中,您可以执行以下操作:
$this->set('user',$this->User->find('all', array ('conditions' => array('email' => $email, 'password' => $password))));
And in your view 在您看来
foreach ($user as $us) {
echo($us['name']);
//your code
}
For your query: 对于您的查询:
<?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
}
?>
Use cakephp find
method to do that. 使用cakephp find
方法可以做到这一点。
<?php
$users = $this->User->find('first',array
(
'conditions' => array
(
'User.email' => $email,
'User.password' => $password
)
));
pr($users);
exit;
?>
i have added pr($users);exit;
我已经添加了pr($users);exit;
for debugging result of query. 用于调试查询结果。
You can also pass recursive
, limit
, order
etc along with find method read more about find
at cakephp 您还可以传递recursive
, limit
, order
等以及find方法, order
在Cakephp中阅读有关find
更多信息。
If you are in Users Controller then it is not necessary to load your Model and connect your database. 如果您在Users Controller中,则无需加载模型并连接数据库。 If you access users Model from another Model then load your User Model in your controller. 如果从另一个模型访问用户模型,则将用户模型加载到控制器中。 You load your model in two ways 您可以通过两种方式加载模型
$this->loadmodel('User');
or
public $uses = array('User');
$results = $this->User->find('all',array('conditions' => array('email' => $email,'password' => $password)));
$this->set(compact('results '));
now in your view file use this 现在在您的视图文件中使用此
foreach ($results as $users) {
echo h($users['User']['email']);
echo h($users['User']['password']);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.