2

I've to tried figure this out for half a day...

I have a script that calculates against the DB to see, if any rows should be deleted.

$number = count($INSERT)-count($INDB);

The $number variable will either be

  1. $number = 0, which means that no rows should be deleted or inserted
  2. $number > 0, which means that we need to insert rows
  3. $nubmer < 0, which means that we need to delete some rows

The 1. and 2. works - but the 3. is giving me an error.

if($number < 0){
    $limit = abs($number);
    echo "DELETE FROM pversions 
          WHERE fk_p_id = $pid 
          ORDER BY pversion_id DESC 
          LIMIT $limit";

    $remVersions = $pdo->prepare("
        DELETE FROM pversions 
        WHERE fk_p_id = :pid 
        ORDER BY pversion_id DESC 
        LIMIT :lmt");
    $remVersions->execute(array(":pid" => $pid, ":lmt" => $limit));

    $left = count($versions)-$limit;
}

The echo could be returning this:

DELETE FROM pversions WHERE fk_p_id = 1 ORDER BY pversion_id DESC LIMIT 1

But this is giving me this PDO Exception:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: 
Syntax error or access violation: 1064 You have an error in your SQL syntax; 
check the manual that corresponds to your MariaDB server version for the right syntax
to use near ''1'' at line 4'

If i take the exact output of the echo from above and enter it in PhpMyAdmin, there is no problem at all. It perform the task exactly like i want it.

If I delete the LIMIT :lmt the error is not showing, and it deletes all the rows. So I'm pretty sure the error is in the "LIMIT".

Hope someone can tell me what I'm doing wrong here.

Lasse
  • 651
  • 2
  • 8
  • 24

0 Answers0