简体   繁体   English

为什么我的PDO语句返回NULL?

[英]Why does my PDO statement return NULL?

For some reason my PDO statement keeps returning NULL when it should return an integer. 出于某种原因,我的PDO语句在应返回整数时始终返回NULL。 I've tried to directly input the query into my DB editor (HeidiSQL) and it works fine. 我尝试将查询直接输入到数据库编辑器(HeidiSQL)中,并且工作正常。

Here's the code: 这是代码:

                private function subscribe_moeder(){

                        $email = $this -> args["email"];

                        if(!filter_var($email, FILTER_VALIDATE_EMAIL))return $this -> errors[] = "Invalid email \n";


#                        /#  $Query = "SELECT * FROM subscribers WHERE email = :email";
#                       /##  $core = Core::getInstance();
#                      /###
#       This works    +####
#                      \###  $res = $core -> db -> prepare($Query);
#                       \##  $res -> bindParam(":email", $email);
#                        \#  $res -> execute();


                        if($found = $res -> fetch(PDO::FETCH_OBJ)){


#                           /#  $Query = "SELECT (SELECT id FROM subscriber_binaries WHERE name='møder') &
#                          /##           (SELECT attribute FROM subscribers WHERE email=:email) as s";
#                         /### 
#       This doesn't     +####  $res = $core -> db -> prepare($Query);
#                         \###  $res -> bindParam(":email", $email);
#                          \##  $res -> execute();
#                           \#  $s = $res -> fetch( PDO::FETCH_OBJ );

                                if($s -> s != 0){

                                        $this -> response = 'Already subscribed';

                                } else {

                                        $this -> response = $s->s;

                                }

                        }

                }

This is likely to be an encoding issue for the following reasons: 由于以下原因,这可能是编码问题:

You select two values and do a binary conjunction. 您选择两个值并进行二进制合取。 If the result is NULL then one of the operands must be NULL . 如果结果为NULL则操作数之一必须为NULL Since the rear part will likely not be NULL , since the row exists (and I hope there is some value in the column), the front part must be NULL . 由于后半部分可能不会为NULL ,因为该行存在(并且我希望列中有一些值),所以前半部分必须为NULL There is a special Danish character in the query, I would therefore assume the row you want to select is not selected at all, making the query performing NULL & xxx , which evaluates to NULL . 查询中有一个特殊的丹麦字符,因此,我假设您根本没有选择要选择的行,从而使查询执行NULL & xxx ,其结果为NULL

Try to set the character set to UFT8 and also save your file as UTF8. 尝试将字符集设置为UFT8,并将文件另存为UTF8。

Also try to execute the first subquery from your sourcecode (not PMA) alone, to validate it returns what you expect. 另外,请尝试仅从源代码 (而非PMA)执行第一个子查询,以验证其是否返回您期望的结果。

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

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