0

I'm new to cakephp how to write below sql query in cakephp

$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

3
  • read the cakePHP docs Commented Feb 22, 2013 at 9:51
  • 2
    why is this even tagged cakephp? Commented Feb 22, 2013 at 9:58
  • coz I need to know how to write normal mysql code in cakephp. I mention it on my answer Commented Feb 22, 2013 at 10:07

5 Answers 5

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

 }
?>

2 Comments

Your code isn't correct you have miss a ' after User and he wants all result not only first
Thanks, that was a typo. I fixed it. And for first/all, please look into the query. For those type of queries, there should be always only one email with password combination. But still there are options to get all, instead of first. I hope I am clear now
1

read this for easy authentication with cakephp framework http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

Comments

1

Use cakephp find method to do that.

<?php

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

    pr($users);
    exit;

?>

i have added 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

Comments

1

If you are in Users Controller then it is not necessary to load your Model and connect your database. 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']);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.