cakephp从db获取数据

时间:2013-02-22 09:51:03

标签: php mysql 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'];
}

请帮帮我 感谢

5 个答案:

答案 0 :(得分:1)

阅读本文以便使用cakephp框架轻松进行身份验证 http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

答案 1 :(得分:1)

在用户控制器中,您可以执行以下操作:

$this->set('user',$this->User->find('all', array ('conditions' => array('email' => $email, 'password' => $password))));

在你看来

foreach ($user as $us) {
   echo($us['name']);
//your code
}

答案 2 :(得分:1)

查询:

<?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

 }
?>

答案 3 :(得分:1)

使用cakephp find方法来做到这一点。

<?php

    $users = $this->User->find('first',array
    (
        'conditions' => array
        (
            'User.email'    => $email,
            'User.password' => $password
        )
    ));

    pr($users);
    exit;

?>

我添加了pr($users);exit;来调试查询结果。

您还可以传递recursivelimitorder等以及查找方法,详细了解cakephpfind

答案 4 :(得分:0)

如果您在用户控制器中,则无需加载模型并连接数据库。如果从另一个模型访问用户Model,则在控制器中加载用户模型。您以两种方式加载模型

$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']);
}