0

I want to compare an array element to wether 0 or 1. For that I want to select my is_answer_accepted that is returned by the function is_answer_accepted($aid)

if($is_answer_accepted['is_answer_accepted'] === 0) {
    delete_all_answer_as_solved($_GET['question']);
    mark_question_as_solved($_GET['answer'], $_GET['question']);
}

if($is_answer_accepted['is_answer_accepted'] === 1) { //Debugger message: $is_answer_accepted: {is_answer_accepted => 0}[1]
    delete_answer_as_solved($_GET['answer'], $_GET['question']);
}

I'm clicking on a non accepted answer (confirmed), therefore my is_answer_accepted should be 0. Somehow my comparison is failing and $is_answer_accepted['is_answer_accepted'] = 1 is returning true even tho it is certainly 0.

function is_answer_accepted($aid) {
    global $conn;
    $query=$conn->prepare("SELECT is_answer_accepted FROM is_answer_accepted(:aid)");
    $query->execute(array($aid));


    return $query->fetch();
}

When I do this query I return 1 if solved_date != NULL and 0 otherwise.

CREATE OR REPLACE FUNCTION is_answer_accepted(aid INT)
  returns INTEGER
LANGUAGE plpgsql
AS $$
DECLARE date_solved TIMESTAMP;
  DECLARE answer_accepted INTEGER;
BEGIN
  SELECT solved_date FROM answers WHERE answers.publicationid = aid
  INTO date_solved;

  IF date_solved is null THEN
    answer_accepted := 0;
  ELSE
    answer_accepted :=1;
  END IF;

  return answer_accepted;
END
$$;

The response is:

is_answer_accepted
------------------
                 0
(1 row)

Any idea how the comparison is wrong?

Kind regards

vftw
  • 1,547
  • 3
  • 22
  • 51
  • Possible duplicate of [The 3 different equals](http://stackoverflow.com/questions/2063480/the-3-different-equals) – Qirel May 12 '17 at 11:29
  • 1
    `if($is_answer_accepted['is_answer_accepted'] = 0) {` means you *assign* the value 0 to the variable. See the flagged dupe. You need to compare, using `==` or `===` (strict comparison). – Qirel May 12 '17 at 11:30
  • `=` declares variables, `==` will do a loose comparison and `===` is a strict comparison. I can't remember the ins and outs but I think the `==` does something with dataTypes where `===` has to match dataType and value. – treyBake May 12 '17 at 11:31
  • @ThisGuyHasTwoThumbs like the dupe says, `==` will only compare *value*. That might get you some funky responses, because `"string" == true` will actually return a true comparison. `===` needs the same value **and** the same data-type. So `"1" === 1` will fail, because it's a string and an integer. – Qirel May 12 '17 at 11:33
  • @Qirel ah so my hunch was right - just not confirmed haha thanks :) – treyBake May 12 '17 at 11:37
  • My bad, I had `===` , I changed for some desperate debugging. I edited the original content. It still does the incorrect comparison though – vftw May 12 '17 at 11:38
  • `var_dump($is_answer_accepted['is_answer_accepted'])` and check exactly what it is. – Qirel May 12 '17 at 11:44

1 Answers1

0
if($is_answer_accepted['is_answer_accepted'] == 0) {
    delete_all_answer_as_solved($_GET['question']);
    mark_question_as_solved($_GET['answer'], $_GET['question']);
}

if($is_answer_accepted['is_answer_accepted'] == 1) { //Debugger message: $is_answer_accepted: {is_answer_accepted => 0}[1]
    delete_answer_as_solved($_GET['answer'], $_GET['question']);
}

your if statement include assigning not comparssion

Osama
  • 2,912
  • 1
  • 12
  • 15
  • 1
    Please avoid answering questions which are clearly duplicates of existing questions. Also, a code-dump without an explanation isn't a good answer. – Qirel May 12 '17 at 11:33