0

I have a series of MySql queries that need to be called with PDO. Many of them are using MySql variables. Surely, a solution would be to use PDO prepared statements but that's not always the case. Insert and Update statements are running with no problems but Select is throwing an error. (I'm unable to paste it for you right now. Please say if it's necessary)

Select queries are running fine when executed in MySQL Workbench.

SET 
    @CY = YEAR(DATE_ADD(CURRENT_DATE,INTERVAL -17 DAY));

SELECT
   *
FROM
    TableX
WHERE
    TableX.Year IN (@CY, @CY-1)

Any ideas?

Additional info: PHP 5.3.29 and MYSQL 5.5.47

1
  • What error is the SELECT throwing? Commented Feb 1, 2016 at 15:52

1 Answer 1

2

You may execute similar queries with two steps:

$stmt = $pdo->query('SET @CY = YEAR(DATE_ADD(CURRENT_DATE,INTERVAL -17 DAY));');
$stmt = $pdo->query('SELECT @CY, @CY-1');
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($data);

Outputs:

Array
(
    [0] => Array
        (
            [@CY] => 2016
            [@CY-1] => 2015
        )

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