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; } } } 
10
  • 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 :) Commented Jul 16, 2014 at 12:47
  • What happens if you add $core -> db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); before the first prepare? Commented Jul 16, 2014 at 12:48
  • @JorgeCampos unfortunately no, it just returns a NULL field Commented Jul 16, 2014 at 12:49
  • 1
    I must say, that's one beautiful formatting you did there. Commented Jul 16, 2014 at 13:03
  • 1
    Although the formatting is kind of pretty, it intends the code so much, that it's hard to read. Commented Jul 25, 2014 at 9:09

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.