1

I got the following sql:

SELECT COUNT(pu_id) FROM purchasing WHERE MONTH(pu_create_date)=MONTH(NOW())

In Mysql it gives the result 0 as expected.

When I put it in PHP I get the result 1 of $numMonth. This is the php code:

$database = new Database();
$db = $database->getConnection();

$stmt= $db->query('SELECT COUNT(pu_id) FROM purchasing WHERE MONTH(pu_create_date)=MONTH(NOW())') ;
$numMonth = $stmt->execute();
echo $numMonth;

Why do I get two different results?

pu_id = unique key

pu_create_date = timestamp

4
  • 2
    You're not fetching the results of the query. Commented Jun 29, 2020 at 18:10
  • Are you using PDO or mysqli? Commented Jun 29, 2020 at 18:11
  • 1
    in this instance 1 is true as it is the boolean response from the execute method Commented Jun 29, 2020 at 18:12
  • Im using PDO for this Commented Jun 29, 2020 at 18:12

1 Answer 1

3

You have to fetch the row from the query.

$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_NUM);
$numMonth = $row[0];
echo $numMonth;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.