php - YII CActiveRecord->find()

标签 php yii yii-cactiverecord

我现在仍在通过博客教程学习 YII,并且对一些代码感到好奇。

在这个链接上
http://www.yiiframework.com/doc/blog/1.1/en/prototype.auth

有这样的代码

<?php
class UserIdentity extends CUserIdentity
{
private $_id;

public function authenticate()
{
    $username=strtolower($this->username);
    $user=User::model()->find('LOWER(username)=?',array($username));
    if($user===null)
        $this->errorCode=self::ERROR_USERNAME_INVALID;
    else if(!$user->validatePassword($this->password))
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else
    {
        $this->_id=$user->id;
        $this->username=$user->username;
        $this->errorCode=self::ERROR_NONE;
    }
    return $this->errorCode==self::ERROR_NONE;
}

public function getId()
{
    return $this->_id;
}
}

我对一些代码很好奇。

  1. 为什么代码最后一行没有?>
  2. 在这一行 $user=User::model()->find('LOWER(username)=?',array($username)); 为什么使用 LOWER(username )=? 不是 LOWER(username)=。为什么需要 ,这是我可能还不知道的某些查询条件吗?

最佳答案

    根据 this link
  1. ?> 并不是真正需要的:

    The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.

  2. ? 与 SQL 语法相关,如 here 所示.还有 second answer here说:

    The question mark represents a parameter that will later be replaced. Using parameterized queries is more secure than embedding the parameters right into the query.

关于php - YII CActiveRecord->find(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7567686/

相关文章:

javascript - 如何在 XMLHttpRequest 中添加 PHP 回显的元素?

caching - Yii半静态站点优化

php - 具有自定义值的 CActiveDataProvider

forms - 在 Yii 中,如何最好地将搜索结果选择和之前输入的表单模型发布回表单 Controller ?

php - 合并 php 数组和 php 对象

php - Yii2 独特验证器在插入速度太快时无法工作

php - 伊伊 : how to select 1 registration using the best method

php - sqs 按值升序排列

javascript - 我可以从 html 获取文本并将其放入 php 中的变量而不提交表单吗?

php - 使用 PHP mySQL simpleHTMLDOM 插入数据时调用非对象上的成员函数 find()