Question

My query

$stmt = $db->prepare('SELECT * FROM generalevent ORDER BY date DESC LIMIT ?, 25');
$stmt->execute(array( $limit ));

keeps failing with the message

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 
MySQL server version for the right syntax to use near '?, 25' at line 1' 
in ../test.php:35 
Stack trace: #0 ../test.php(35): PDO->prepare('SELECT * FROM g...') #1 {main}

I already have

$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

so from what I have read, this should work, no?

No correct solution

OTHER TIPS

The following worked for me, however it's hard to give a definite answer, since full code was not provided.

Here is what I tested with, on my own server from an existing table.

Sidenote: Using :limit or ? after LIMIT both worked and did not throw any errors.

<?php
$mysql_username = 'xxx'; // for DB
$mysql_password = 'xxx'; // for DB

try {

$pdo= new PDO('mysql:host=localhost;dbname=database_name', $mysql_username, $mysql_password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );

} catch (PDOException $e) {
     exit( $e->getMessage() );
}

try {

$currPage = 2;

// $limit = 1;

$limit = $currPage * 25;

$sql = "SELECT * FROM animals LIMIT :limit,25";

$stmt = $pdo->prepare($sql);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();

     $results = $pdo->query($sql);

//     print_r($results);
} catch (PDOException $e) {
     echo $e->getMessage();
}

    $cols = $results->columnCount(); // Number of returned columns

    echo 'Number of returned columns: '. $cols. '<br />';

echo '

<div align="center">
  <center>
  <table border="1" cellspacing="0" cellpadding="3">
    <tr>

      <td width="10%" bgcolor="#99CCFF"><p align="center">ID</td>
      <td width="33%" bgcolor="#99CCFF"><p align="center">Breed</td>
      <td width="34%" bgcolor="#99CCFF"><p align="center">Species</td>
    </tr>

';

foreach($results as $row) {

echo "<tr><td width=\"10%\"><p align=\"center\">" . $row['id'] . "</td>\n<td width=\"33%\">" . $row['name'] . "</td><td width=\"33%\">" . $row['species'] . "</td>";

}

echo "\n";
  echo "</tr>";

echo '

  </table>
  </center>
</div>
';

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top