9

I am unable to execut long script the pdo throws an exception:

SQLSTATE[HY000]: General error

If I submit script which does not contain variables it runs w/o problem. The same script runs on phpmyadmin interface.

Here is my code snippet:

 try {
 $dsn = "mysql:host=" . DB_SERVER . ";dbname=" . DB_DEFAULT;
 $db = new PDO($dsn, DB_USER, DB_PASS);
 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 $q = $db->query($query);
 if (!$q) {
 echo $db->errorInfo();
  } else {
        $rows = $q->fetchAll(PDO::FETCH_ASSOC);
    }
} catch (PDOException $e) {
    var_dump($e);
 }

Here is some test which does not execute by PDO:

SET @ra_LMC:=80.9;
SELECT @ra_LMC;

How I should execut with pdo the multi line scripts?

Thanks
Arman.

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Arman
  • 4,566
  • 10
  • 45
  • 66

2 Answers2

14

PDO does not allow the execution of multiple statements in one query() request. But your @ra_LMC variable should be visible in the current connection, so you can put your second line (SELECT) into a new query() call.

To read a whole script, you have to parse the file and run each statement with a call to query().

cytrinox
  • 1,846
  • 5
  • 25
  • 46
7

PDO can only execute one statement at a time. You can ether run the SET and SELECT as 2 separate statements. Or you can set the variable using FROM.

SELECT @ra_LMC FROM (SELECT @ra_LMC:=80.9) q
gen_Eric
  • 223,194
  • 41
  • 299
  • 337