0

For some reason my PDO statement keeps returning NULL when it should return an integer. I've tried to directly input the query into my DB editor (HeidiSQL) and it works fine.

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;

                                }

                        }

                }
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
Caweren
  • 236
  • 1
  • 9
  • 3
    Don't use pastebin for code. Just pop it directly into your question, it makes it easier for you to get an answer, and it also means that id pastebin falls off the face of the internet tomorrow, your question can still remain here and be readable and make sense :) – Fluffeh Jul 16 '14 at 12:47
  • What happens if you add `$core -> db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` before the first prepare? – VolkerK Jul 16 '14 at 12:48
  • @JorgeCampos unfortunately no, it just returns a NULL field – Caweren Jul 16 '14 at 12:49
  • Does this query `(SELECT id FROM subscriber_binaries WHERE name='møder')` returns only one row ? – Jorge Campos Jul 16 '14 at 12:49
  • What is returned when you execute the query in PMA or other mysql client? – bksi Jul 16 '14 at 12:50
  • @VolkerK The Query does not actually return an error (i've tried with the errmode attributes), the query runs perfectly as expected. But i did not expect it to return NULL. – Caweren Jul 16 '14 at 12:50
  • @bksi yes, both subqueries will always return 1 row – Caweren Jul 16 '14 at 12:51
  • 1
    I must say, that's one beautiful formatting you did there. – Michal Jul 16 '14 at 13:03
  • I mean did you try the whole query, not only the subqueries? – bksi Jul 16 '14 at 13:06
  • 1
    Although the formatting is kind of pretty, it intends the code so much, that it's hard to read. – Ulrich Thomas Gabor Jul 25 '14 at 09:09

1 Answers1

2

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

Try to set the character set to UFT8 and also save your file as UTF8.

Also try to execute the first subquery from your sourcecode (not PMA) alone, to validate it returns what you expect.

Community
  • 1
  • 1
Ulrich Thomas Gabor
  • 6,584
  • 4
  • 27
  • 41
  • The answer you gave might've worked. But i've since moved on and am now storing subscriber informations differently. I figured that using the binary selector would only let the user subscribe to 64 different newsletters. I can't mark you answer is the solution, since i can't actually test it anymore :( Have an upvote though. – Caweren Jul 25 '14 at 11:52