简体   繁体   English

为什么这个'case'查询在codeigniter中不返回'true'或'false'

[英]Why does this 'case' query not return 'true' or 'false' in codeigniter

I have a query that checks if a row from a table exists. 我有一个查询,用于检查表中是否存在行。 It should return a 'true' or 'false' value, but this is not the case. 它应返回“ true”或“ false”值,但事实并非如此。

The query and code look as follows. 查询和代码如下。 I should mention that I use the CodeIgniter framework, hence the object names and function names. 我应该提到我使用CodeIgniter框架,因此使用对象名称和函数名称。

$query="SELECT CASE WHEN EXISTS
                (
                SELECT * FROM Users
                WHERE Email=".$this->db->escape($email)."
                AND PassWord=MD5(".$this->db->escape($password).")
                )
                THEN 'TRUE'
                ELSE 'FALSE'
                END";

            $result=$this->users_db->query($query);  
            $resulting_array=$result->row();

            echo "<pre>".var_dump($resulting_array)."</pre>";

This code gives the following result: 此代码给出以下结果:

object(stdClass)#22 (1) {
  ["CASE WHEN EXISTS
                (
                SELECT * FROM Users
                WHERE Email='r.blaauwen@erasmusmc.nl'
                AND PassWord=MD5('rrt')
                )
                THEN 'TRUE'
                ELSE 'FALSE'
                END"]=>
  string(5) "FALSE"
}

It seems $result->row(); 看来$result->row(); delivered an object instead of an array/string/boolean. 提供了一个对象,而不是数组/字符串/布尔值。 The 'FALSE' result is there, but I don't know how to retrieve it. “ FALSE”结果在那里,但是我不知道如何检索它。

You can make a alias in your sql like this: 您可以像这样在sql中创建别名:

$query="SELECT CASE WHEN EXISTS
                (
                SELECT * FROM Users
                WHERE Email=".$this->db->escape($email)."
                AND PassWord=MD5(".$this->db->escape($password).")
                )
                THEN 'TRUE'
                ELSE 'FALSE'
                END 
         AS my_result";

$result=$this->users_db->query($query);  
$resulting_array=$result->row();

echo $resulting_array->my_result;

Or you can do this: 或者您可以这样做:

$resulting_array=$result->row();
//Getting the properties of the given object
$props = get_object_vars($resulting_array);
//Gets the name of the property
$name = array_keys($props)[0];

echo $resulting_array->$name;

MySQL has no boolean type, so if you want to treat the response as boolean, you should use 0 or 1 . MySQL没有布尔类型,因此如果要将响应视为布尔值,则应使用01

Next, CodeIgniter's database class is returning a standard object, but it's not very accessible because you're selecting something that isn't named. 接下来,CodeIgniter的数据库类返回一个标准对象,但是由于您选择的是未命名的对象,因此它不是很容易访问。 If you alias the field, then you can access it easier: 如果为该字段加上别名,则可以更轻松地访问它:

$query="SELECT (CASE WHEN EXISTS
            (
              SELECT * FROM Users
              WHERE Email=".$this->db->escape($email)."
              AND PassWord=MD5(".$this->db->escape($password).")
            )
            THEN 1
            ELSE 0
            END
        ) AS userExists";
$result=$this->users_db->query($query);  
$resulting_array=$result->row();

if ($resulting_array->userExists) {
    echo "User Exists!";
} else {
    echo "Invalid password/no user";
}

Finally, using MD5 to hash passwords is a really bad idea . 最后,使用MD5哈希密码是一个非常糟糕的主意 Take a read of the official PHP documentation about passwords to see why: 阅读有关密码官方PHP文档,以了解原因:

Why are common hashing functions such as md5() and sha1() unsuitable for passwords? 为什么常见的哈希函数(例如md5()和sha1())不适合密码?

Hashing algorithms such as MD5, SHA1 and SHA256 are designed to be very fast and efficient. 诸如MD5,SHA1和SHA256之类的哈希算法被设计为非常快速和高效。 With modern techniques and computer equipment, it has become trivial to "brute force" the output of these algorithms, in order to determine the original input. 随着现代技术和计算机设备的出现,“蛮力”将这些算法的输出确定原始输入变得微不足道了。

Because of how quickly a modern computer can "reverse" these hashing algorithms, many security professionals strongly suggest against their use for password hashing. 由于现代计算机能够快速“逆向”这些哈希算法,因此许多安全专业人员强烈建议不要将其用于密码哈希。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM